【发布时间】:2018-08-05 01:07:12
【问题描述】:
我正在做一个附加复选框值的简单代码,我遇到了这个错误。为什么显示此错误对我来说是个谜。我已经想出了正确编写它的方法,但我非常想知道为什么会发生这个错误?尝试了之前提出的问题中给出的技术,但没有奏效-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>
Correct Code:
$(document).ready(function(){
$("input[type='checkbox']").click(function(){
$("#msg").html(" ");
$("input[type='checkbox']: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>
【问题讨论】:
-
无论如何,您认为
is函数会返回什么?您如何期望您能够致电each? -
在我的情况下返回
true -
那你为什么认为
true有一个成员函数each? -
提示:必要时查阅 jQuery 的文档,而不是试图记住所有内容。
-
$("input[type='checkbox']").is(':checked')返回一个布尔值 (true/false)。$("input[type='checkbox']:checked")返回一个 jQuery 元素的集合,您可以使用$.each对其进行迭代。
标签: jquery