改變checkbox預設選中狀態及取值
阿新 • • 發佈:2019-02-08
<inputtype="checkbox"name="chk"value="1"val="1級">1級
<inputtype="checkbox"name="chk"value="2"checked="checked"val="2級">2級
<inputtype="checkbox"name="chk"value="3"val="3級">3級
1)設定1級和3級選中
2)獲取選中的值,以‘,’分隔顯示
更改預設狀態:
$(":checkbox").attr("checked", function() {
if ($(this).is(":checked")) {
return false;
} else {
return true;
}
});
遍歷取值:
$(":checkbox").click(function() {
var arrayValue = new Array();
$(":checkbox").each(function(){
if ($(this).is(":checked")) {
arrayValue.push($(this).val());
}
});
arrayValue.join(",");
alert(arrayValue);
});