最近在寫后臺的時候,需要一個全選打鉤及消除的功能,在此列出,方便各位簡友。
下面是JS代碼:
//檢查input狀態
$("#ckAll").click(function() {
$("input[name='sub']").prop("checked", this.checked);
});
//判斷是否打鉤
$("input[name='sub']").click(function() {
var $subs = $("input[name='sub']");
$("#ckAll").prop("checked" , $subs.length == $subs.filter(":checked").length ? true :false);
});
//全選刪除
$(function(){
$("#delete").click(function() {
text = $("input:checkbox[name='sub']:checked").map(function(index,elem) {
return $(elem).val();
}).get();
alert(text);
});
});
下面是html代碼:
<intput id="ckAll" type="checkbox" />//全選打鉤框
<input type ="checkbox" name ="sub" value="">//選擇打鉤框
<input type="button" id="open" value="提交">