var atLeastOneIsChecked = $('#checkArray:checkbox:checked').length > 0;//there should be no space between identifier and selector
// or, without the container:
var atLeastOneIsChecked = $('input[name="chk[]"]:checked').length > 0;
<input type="checkbox" name="mycheckbox" id="mycheckbox" /><br><br><input type="button" id="test-with-checked" value="Test with checked" /><input type="button" id="test-with-is" value="Test with is" /><input type="button" id="test-with-prop" value="Test with prop" />
示例1-带检查
$("#test-with-checked").on("click", function(){if(mycheckbox.checked) {alert("Checkbox is checked.");} else {alert("Checkbox is unchecked.");}});
示例2-使用jQuery is,注意-:选中
var check;$("#test-with-is").on("click", function(){check = $("#mycheckbox").is(":checked");if(check) {alert("Checkbox is checked.");} else {alert("Checkbox is unchecked.");}});
示例3-使用jQuery prop
var check;$("#test-with-prop").on("click", function(){check = $("#mycheckbox").prop("checked");if(check) {alert("Checkbox is checked.");} else {alert("Checkbox is unchecked.");}});
var checkbox = $('#'+id);/* OR var checkbox = $("input[name=checkbox1]"); whichever is best */
/* The DOM way - The fastest */if(checkbox[0].checked == true)alert('Checkbox is checked!!');
/* Using jQuery .prop() - The second fastest */if(checkbox.prop('checked') == true)alert('Checkbox is checked!!');
/* Using jQuery .is() - The slowest in the lot */if(checkbox.is(':checked') == true)alert('Checkbox is checked!!');
function isCheckedById(id) {return this[id].checked;}
// TEST
function check() {console.clear()console.log('1',isCheckedById("myCheckbox1"));console.log('2',isCheckedById("myCheckbox2"));console.log('3',isCheckedById("myCheckbox3"));}
<label><input id="myCheckbox1" type="checkbox">check 1</label><label><input id="myCheckbox2" type="checkbox">check 2</label><label><input id="myCheckbox3" type="checkbox">check 3</label><!-- label around inputs makes text clickable --><br><button onclick="check()">show checked</button>