CheckBox 选中取值以及回填
html:
<td align="left" style="word-wrap:break-word;word-break:break-all;" colspan="3"> <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>
获取选中值:
1、CheckBox为单选:
$("input:checkbox:checked").val()
或者
$("input:[type=\'checkbox\']:checked").val();
或者
$("input:[name=\'ck\']:checked").val();
2、CheckBox为多选:
$(\'input:checkbox\').each(function() {
if ($(this).attr(\'checked\') ==true) {
alert($(this).val());
}
});
3、全选:
$(\'input:checkbox\').each(function() {
$(this).attr(\'checked\', true);
});
4、全不选:
$(\'input:checkbox\').each(function () {
$(this).attr(\'checked\',false);
});
5、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\');
6、CheckBox只能单选
$(":checkbox").click(function(){
if($(this).is(\':checked\')){
$(this).attr(\'checked\',true).siblings().attr(\'checked\',false);
}
});