## jQuery代码
![mark](http://qiniu.newthink.cc/blog/20171213-164126724.gif)
```
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.bootcss.com/jquery/1.11.3/jquery.js"></script>
</head>
<body>
<ul>
<li><input type="checkbox" name="test"/>看电视</li>
<li><input type="checkbox" name="test"/>看电影</li>
<li><input type="checkbox" name="test"/>上网</li>
<li><input type="checkbox" name="test"/>爬山</li>
<li><input type="checkbox" name="test"/>游乐场</li>
<li><input type="checkbox" name="test"/>逛街</li>
<li><input type="checkbox" name="test"/>聚会</li>
</ul>
<script type="text/javascript">
$('input[type=checkbox]').click(function () {
if ($("input[name='test']:checked").length >= 4) {
$(this).removeAttr("checked");
alert("最多选3个!");
}
});
</script>
</body>
</html>
```
## LayUI
![mark](http://qiniu.newthink.cc/blog/20171213-171523638.gif)
```
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>select获取自定义属性值</title>
<link rel="stylesheet" href="../layui/css/layui.css"/>
</head>
<body class="layui-form">
<input type="checkbox" lay-filter="test" name="test"/>看电视
<input type="checkbox" lay-filter="test" name="test"/>看电影
<input type="checkbox" lay-filter="test" name="test"/>上网
<input type="checkbox" lay-filter="test" name="test"/>爬山
<input type="checkbox" lay-filter="test" name="test"/>游乐场
<input type="checkbox" lay-filter="test" name="test"/>逛街
<input type="checkbox" lay-filter="test" name="test"/>聚会
<script src="../layui/layui.js"></script>
<script>
//一般直接写在一个js文件中
layui.use(['layer', 'form', 'jquery'], function () {
var layer = layui.layer
, form = layui.form
, $ = layui.jquery;
form.on('checkbox(test)',function (data) {
if ($("input[name='test']:checked").length >= 4) {
$(data.elem).removeProp('checked'); //或者$(this)
form.render();
alert("最多选3个!");
}
})
});
</script>
</body>
</html>
```
![mark](http://qiniu.newthink.cc/blog/20171213-171407060.png)