通过点击事件选中多选框

参考:https://blog.csdn.net/qq_35370002/article/details/107913331
 
复制下面代码使用测试
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>

<!-- 
    forEach()方法使用
    语法:array.forEach(callback(currentvalue,index,arr) ,thisValue)
    其中
    callback为数组中每个元素执行的函数,该函数可接受1-3个参数:

      currentvalue参数表示数组的当前元素项,必须的参数
      index参数表示的当前元素下标,可选参数
      arr参数表示当前元素所属的数组,可选参数

    thisValue表示执行回调函数callback()时的this指向。可选参数。当不写时,则默认是指向window全局



    forEach()是没有返回值的!

    forEach适用于只是进行集合或数组遍历,for则在较复杂的循环中效率更高

    

 -->

<script>
$(document).ready(function(){
   
  $("#btn").click(function(){
    debugger;
    var array = [0,1,2,3,4];
    array.forEach(function(item,index){
        alert("数组第"+(index+1)+"个元素是"+item);
        if(item == "0" ){
            $("#btype0").prop("checked",true);
        }
        if(item == "1" ){
            $("#btype1").prop("checked",true);	
        }
        if(item == "2" ){
            $("#btype2").prop("checked",true);	
        }
        if(item == "3" ){
            $("#btype3").prop("checked",true);	
        } 
        if(item == "4" ){
            $("#btype4").prop("checked",true);	
 	 	} 
    });
  });


});
</script>
</head>

<body>
<p>多选框的选择</p>

<input type="checkbox" id="btype0" name="business_type"  value="0" > 苹果 
<input type="checkbox" id="btype1" name="business_type"  value="1" > 香蕉 
<input type="checkbox" id="btype2" name="business_type"  value="2" > 梨子 
<input type="checkbox" id="btype3" name="business_type"  value="3" > 西瓜 
<input type="checkbox" id="btype4" name="business_type"  value="4" > 其他 
	    	
<br/>
<br/>
<br/>
<button id="btn" >点击自动选择多选框</button>

</body>
</html>

  

posted @ 2022-08-09 17:29  Amy清风  阅读(170)  评论(0编辑  收藏  举报