本文摘自PHP中文网,作者coldplay.xixi,侵删。
jquery判断radio是否选中的方法:1、利用获取选中值判断选中;2、使用checked属性判断选中;3、
jquery获取radio单选按钮的值;4、设置单选按钮被选中。

本教程操作环境:windows7系统、jquery3.2.1版本、thinkpad t480电脑。
推荐:jquery视频教程
jquery判断radio是否选中的方法:
一、利用获取选中值判断选中
直接上代码,别忘记引用JQuery包
代码如下:
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 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns= "http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" />
<title>JQuery radio</title>
<script type= "text/javascript" language= "javascript" src= "JavaScript/jquery-1.6.1.min.js" ></script>
<script type= "text/javascript" language= "javascript" >
$( function (){
$( "#btnSubmit" ).click( function (){
var val=$( 'input:radio[name="sex"]:checked' ).val();
if (val==null){
alert( "什么也没选中!" );
return false;
}
else {
alert(val);
}
var list= $( 'input:radio[name="list"]:checked' ).val();
if (list==null){
alert( "请选中一个!" );
return false;
}
else {
alert(list);
}
});
});
</script>
</head>
<body>
<form id= "form1" >
<input type= "radio" name= "sex" value= "男" />男
<input type= "radio" name= "sex" value= "女" />女
<br />
<input type= "radio" name= "list" value= "十分满意" />十分满意
<input type= "radio" name= "list" value= "满意" />满意
<input type= "radio" name= "list" value= "不满意" />不满意
<input type= "radio" name= "list" value= "非常差" />非常差
<br />
<input type= "submit" value= "submit" id= "btnSubmit" />
</form>
</body>
</html>
|
radio不能用“checked”相等来判断,只用用true来判断
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <script type= "text/javascript" >
$( function () {
$( "input" ).click( function () {
if ($(this).attr( "checked" )) {
alert( "选中了" );
}
});
});
</script>
</head>
<body>
<input type= "radio" />
</body>
</html>
|
二、使用checked属性判断选中
radio不能用“checked”相等来判断,只用用true来判断
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <script type= "text/javascript" >
$( function () {
$( "input" ).click( function () {
if ($(this).attr( "checked" )) {
alert( "选中了" );
}
});
});
</script>
</head>
<body>
<input type= "radio" />
</body>
</html>
|
三、jquery获取radio单选按钮的值
另:判断radio是否选中并取得选中的值
代码如下:
1 2 3 4 5 6 7 | function checkradio(){
var item = $( ":radio:checked" );
var len=item.length;
if (len>0){
alert( "yes--选中的值为:" +$( ":radio:checked" ).val());
}
}
|
四、设置单选按钮被选中
代码如下:
1 | $( "input[type=radio]" ).attr( "checked" , '2' );
|
相关免费学习推荐:javascript(视频)
以上就是jquery怎么判断radio是否选中的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
jQuery ui datepicker时间控件的用法(一)
jQuery ajax post 回调函数不执行怎么办
如何使用jQuery 消除网页的滚动条
jQuery用什么方法清空input值
jQuery怎么操作css设置高度
jQuery基本过滤器有哪些
jQuery的ajax()函数传值中文乱码怎么办
jQuery 如何判断 是否相等
jQuery如何获取祖先元素
jQuery判断是否出现滚动条的方法
更多相关阅读请进入《jQuery》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » jquery怎么判断radio是否选中