checkbox的各種操作

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/>  



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章