Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

jquery - Can't understand why this error(Uncaught TypeError: $(...).is(...).each is not a function) got on?

I was doing a simple code of appending the values of checkboxes and I encountered this error. It is a mystery for me why this error was shown. I have figured out the way to write it correctly but I would very much like to know why this error occurred? Tried the technique given in a question previously asked but it didn't work- jquery - is not a function error

Code with error:

$(document).ready(function(){
	$("input[type='checkbox']").click(function(){
		
		$("#msg").html(" ");
		
		 $("input[type='checkbox']").is(':checked').each(function(){
			$("#msg").append(this.value);
			
			})
		})	
	
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="fruits[]" value="mango">mango
<input type="checkbox" name="fruits[]" value="apple">apple
<input type="checkbox" name="fruits[]" value="cherry">cherry
<div id="msg"></div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

$("input[type='checkbox']").is(':checked') is a test that returns a Boolean (true/false). true.each() does not make sense, neither does false.each().

$("input[type='checkbox']:checked") returns a collection of jQuery elements that you can iterate over using $.each.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...