jquery的复选框的取值
<div>
请选择你喜欢的运动
<input type="checkbox" name="hobby" value="1"/>篮球
<input type="checkbox" name="hobby" value="2"/>足球
<input type="checkbox" name="hobby" value="3"/>排球
<button id="gethobby">取值</button>
<button id="sethobby">设置默认选中</button>
</div>
$("#sethobby").click(function(){
// $("input[name=hobby][value=1],input[name=hobby][value=3]").prop("checked",true);
// $("input[name=hobby]").each(function(index,item){
// if($(item).val()==1||$(item).val()=="3"){
// $(item).prop("checked",true);
// }
// })
let str = "1_2_3";
let hobbyList = str.split("_");//[2,3]
$("input[name=hobby]").each(function(index,item){//[1,2,3]
if(hobbyList.indexOf($(item).val())!=-1){
$(item).prop("checked",true);
}
})
});
浙公网安备 33010602011771号