通过点击事件选中多选框

参考:https://blog.csdn.net/qq_35370002/article/details/107913331
 
复制下面代码使用测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<!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 @   Amy清风  阅读(172)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示