<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("input[type=checkbox]").click(function(){
var checkedStatus = $(this).prop("checked");
var groupid = $(this).attr('groupid');
alert('' + checkedStatus);
if(true==checkedStatus){
$('input[type=checkbox][groupid=' + groupid + ']').prop('checked', true);
}else{
$('input[type=checkbox][groupid=' + groupid + ']').prop('checked', false);
}
});
});
</script>
</head>
<body>
<form action="">
名字: <input type="text" name="user"><br>
我有一辆自行车:
<input type="checkbox" name="vehicle" groupid="2345" value="Bike"><br>
我有一辆汽车:
<input type="checkbox" name="vehicle" groupid="2345" value="Car"><br>
我有一架飞机:
<input type="checkbox" name="vehicle" groupid="2345" value="Airplane"><br>
<input type="submit">
</form>
<p>注意:我们使用jQuery .wrap 方法来设置选中的元素高亮,Firefox 浏览器不支持复选框设置背景颜色。</p>
</body>
</html>
在项目中使用了jquery.uniform.js表单插件,则会使事件失效,
$("input[name=voucherType][type=radio][value=2413010]").attr("checked",true);
需加入 $.uniform.update($('input[name=voucherType]'));
百度看到有人说这样也可以,具体没试过$.uniform.update($("#check2").attr("checked", true));