Javascript jQuery-检查复选框值(链接jQuery函数与If语句)
我有多个复选框,希望得到它们的总体值 例如,如果选中一个,则该值为真/选中 如果未选中任何选项,则为false/unchecked 我的HTML是:Javascript jQuery-检查复选框值(链接jQuery函数与If语句),javascript,jquery,html,checkbox,Javascript,Jquery,Html,Checkbox,我有多个复选框,希望得到它们的总体值 例如,如果选中一个,则该值为真/选中 如果未选中任何选项,则为false/unchecked 我的HTML是: <input id="one" type="checkbox"> <input id="two" type="checkbox"> <input id="three" type="checkbox"> <input id="four" type="checkbox"> <button oncli
<input id="one" type="checkbox">
<input id="two" type="checkbox">
<input id="three" type="checkbox">
<input id="four" type="checkbox">
<button onclick="check();">Is checked (jQuery Chained)</button>
<button onclick="check2();">Is checked(Big If Statement)</button>
Js小提琴:
请注意,我已经解决了这个问题。这个问题更能帮助我理解为什么我的checked2()
函数工作而我的check()
不工作代码>仅检查是否选中了第一个(在本例中为#one
)复选框
从
获取集合中第一个元素的属性值
匹配元素
应该是
var booleee = $('#one,#two,#three,#four').filter(':checked').length > 0;
此外,在新版本(>1.6)中,您需要使用属性“checked”(.prop('checked')
)而不是attr
,在替代方案中,您有一个so.is(':checked')
var booleee=$('one,'two,'two,'two,'two,'two,'two').attr('checked')代码>仅检查是否选中了第一个(在本例中为#one
)复选框
从
获取集合中第一个元素的属性值
匹配元素
应该是
var booleee = $('#one,#two,#three,#four').filter(':checked').length > 0;
此外,在新版本(>1.6)中,您需要使用属性“checked”(.prop('checked')
)而不是attr
,在html中您有一个so.is(':checked')
设置checked属性
<input id="one" type="checkbox" checked="checked">
<input id="two" type="checkbox" checked="checked">
<input id="three" type="checkbox" checked="checked">
<input id="four" type="checkbox" checked="checked">
您没有定义checked,所以它显示undefined,因为它无法读取此属性,当您调用第二个函数时,只有false会发出警报,因为它们的checkbox checked属性找不到
请参见html集合检查属性中的
<input id="one" type="checkbox" checked="checked">
<input id="two" type="checkbox" checked="checked">
<input id="three" type="checkbox" checked="checked">
<input id="four" type="checkbox" checked="checked">
您没有定义checked,所以它显示undefined,因为它无法读取此属性,当您调用第二个函数时,只有false会发出警报,因为它们的checkbox checked属性找不到
请参见,因为它没有检查选择器中的所有元素
让它工作
function check() {
var booleee =$("input:checkbox:checked").length > 0;
alert("Checked: " + booleee);
}
要进行更具体的选择,请在选择器中添加表单id
因为它没有检查选择器中的所有元素
让它工作
function check() {
var booleee =$("input:checkbox:checked").length > 0;
alert("Checked: " + booleee);
}
要进行更具体的选择,请在选择器中添加表单id
更好的方法:
更好的方法: