JQuery判断radio单选框是否选中

论坛 期权论坛 脚本     
匿名技术用户   2021-1-6 08:48   86   0
摘要 在开发中我们往往需要对某单选框或复选框的选中状态做判断,看了看网上有的写的好麻烦好麻烦,最标准的也是最简单就是用is()来进行判断

在开发中我们往往需要对某单选框或复选框的选中状态做判断,看了看网上有的写的好麻烦好麻烦,最标准的也是最简单就是用is()来进行判断:

Jquery版本,请确保你已经导入jquery:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<div>
<input type= "radio" name= "check" id= "first" >
<input type= "radio" name= "check" id= "second" >
<input type= "radio" name= "check" id= "third" >
</div>
<script type= "text/javascript" >
$( function (){
//获取radio集
$( "input:radio[name='check']" ).change( function (){
//遍历radio集
$( "input:radio[name='check']" ).each( function (){
//当前radio是否为选中状态
alert($(this).attr( 'id' )+ ' is ' +$(this).is( ':checked' ));
});
});
});
</script>

JS版本:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<div>
<input type= "radio" name= "check" id= "first" >
<input type= "radio" name= "check" id= "second" >
<input type= "radio" name= "check" id= "third" >
</div>
<button onclick= "checkStatus()" >JS测试选中状态</button>
<!--js版本-->
<script type= "text/javascript" >
function checkStatus(){
var radios = document.getElementsByName( 'check' );
for ( var i = 0; i < radios.length; i ++) {
if (radios[i].checked) {
alert(radios[i].getAttribute( 'id' )+ ' is checked!' );
} else {
alert(radios[i].getAttribute( 'id' )+ ' is not checked!' );
}
}
}
</script>
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP