我有多个复选框,并希望为他们获得一个整体值。jQuery - 检查复选框值(链接jQuery函数如果声明)
例如,如果选中一个,则值为真/被选中。
如果没有选中,则选择false/unchecked。
我的HTML是:
<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>
我的JavaScript/jQuery是:
function check() {
var booleee = $('#one,#two,#three,#four').attr('checked');
alert("Checked: " + booleee);
}
function check2() {
if ($('#one').attr('checked') || $('#two').attr('checked') || $('#three').attr('checked') || $('#four').attr('checked')) {
alert("Checked: true");
}
alert("Checked: false");
}
的js小提琴:Click Here
请注意,我已经解决了这个问题。这个问题更多的是帮助我理解为什么我的checked2()
函数能够正常工作,而我的check()
不能。