<fieldset id="checkArray"> <input type="checkbox" name="chk[]" value="Apples" /> <input type="checkbox" name="chk[]" value="Bananas" /> </fieldset>
查找方法:
var atLeastOneIsChecked = $('input[name="chk[]"]:checked').length > 0;
或者:
var atLeastOneIsChecked = $('#checkArray:checkbox:checked').length > 0;
遍历:
$("#checkArray:checkbox:checked").each(function() { });
如果checkbox的直接父亲不是checkArray,那么上述方法不起所用,可以用find:
$("#checkArray").find(":checked").each(function() { })
或者:
$("#checkArray").find(":checkbox:checked").each(function() { })
radio:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="myForm"> <input type="radio" name="radioName" value="1" /> 1 <br /> <input type="radio" name="radioName" value="2" /> 2 <br /> <input type="radio" name="radioName" value="3" /> 3 <br /> </form>
查看checked方法:
$('input[name=radioName]:checked', '#myForm').val();
$("#myform input[type='radio']:checked").val();
var myRadio = $('input[name=radioName]'); var checkedValue = myRadio.filter(':checked').val();
var form = $('#myform'); var checkedValue = form.find('input[name=radioName]:checked').val();