html:
<td>
<input name="ck" type="checkbox" value="1"/><span>二哈</span>
<input name="ck" type="checkbox" value="2"/><span>泰迪</span>
<input name="ck" type="checkbox" value="3"/><span>柯基</span>
</td>
引用jQuery.min.js
Js:
CheckBox为单选:
$("input:[type= radio]:checked").val();
或者
$("input: radio:checked").val()
或者
$("input:[name='ck']:checked").val();
输出如下图所示
CheckBox为多选:
$("input:checkbox").each(function () {
$(this).click(function () {
//console.log($(this))
if ($(this)[0].checked == true) {
console.log($(this).val());
}
})
})
输出如下图所示
获取全部值
$('input:checkbox').each(function () {
console.log($(this)[0].defaultValue)
});
结果如下图
全选:
$('input:checkbox').each(function() {
$(this).attr('checked', true);
});
全不选:
$('input:checkbox').each(function () {
$(this).attr('checked',false);
});
CheckBox回填:
$('input:checkbox:first').attr("checked",'checked');
或者
$('input:checkbox').eq(‘+索引变量+’).attr("checked",'true');
或者
$('input:checkbox[value='+CheckBox值+']').attr('checked','true');
多个回填:
$('input:radio').slice(0,2).attr('checked','true');
CheckBox只能单选
$(":checkbox").click(function(){
if($(this).is(':checked')){
$(this).attr('checked',true).siblings().attr('checked',false);
}
});