checkbox各种操作,总结
1、checkbox至多选一个
$(function () {
var allBox = $(":checkbox");
allBox.click(function () {
allBox.removeAttr("checked");
$(this).attr("checked", "checked");
});
});
<div>
<input type="checkbox" value="1"/>男
<input type="checkbox" value="2"/>女
</div>
2、得到checkbox选中的值,ajax操作使用
var text="";
$("input[name=items]").each(function() {
if ($(this).attr("checked")) {
text += ","+$(this).val();
}
});
3、checkbox全选或全不选
$("#select").click(function() {
if ($(this).attr("checked")) {
$("input[name=items]").each(function() {
$(this).attr("checked", true);
});
} else {
$("input[name=items]").each(function() {
$(this).attr("checked", false);
});
}
});
<input type="checkbox" id="select"/> 全选<br/>
<input type="checkbox" value="1" name="items">跑步<br/>
<input type="checkbox" value="2" name="items">游泳<br/>
<input type="checkbox" value="3" name="items">滑雪<br/>
<input type="checkbox" value="4" name="items">爬山<br/>
<input type="checkbox" value="5" name="items">徒步<br/>
<input type="checkbox" value="6" name="items">跳伞<br/>