JQuery判断radio(单选框)是否选中和获取选中值方法总结

作者:junjie 时间:2024-04-19 10:24:11 

先给大家分享

JQuery判断radio单选框是否选中并获取值的方法

https://www.aspxhome.com/article/154840.htm

一、利用获取选中值判断选中

直接上代码,别忘记引用JQuery包

<!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"> /*------判断radio是否有选中,获取选中的值--------*/    $(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来判断   

<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来判断   

<script type="text/javascript">        $(function () {            $("input").click(function () {                if ($(this).attr("checked")) {                    alert("选中了");                }            });        });    </script></head><body><input type="radio"/></body></html>


三、jquery获取radio单选按钮的值 

$("input[name='items']:checked").val(); 

另:判断radio是否选中并取得选中的值  如下所示:

function checkradio(){ var item = $(":radio:checked"); var len=item.length; if(len>0){   alert("yes--选中的值为:"+$(":radio:checked").val()); } }

四、获取一组radio被选中项的值  

var item = $('input[name=items][checked]').val();  

五、设置单选按钮被选中


$("input[type=radio]").attr("checked",'2');//设置value=2的项目为当前选中项  


标签:JQuery,radio,单选框,是否选中,选中值
0
投稿

猜你喜欢

  • 这些CSS Selector,你都熟悉吗?

    2008-12-21 16:30:00
  • vue项目中form data形式传参方式

    2024-04-30 10:25:48
  • django框架两个使用模板实例

    2023-11-01 20:05:18
  • python脚本作为Windows服务启动代码详解

    2021-09-11 00:09:26
  • Python文件与文件夹常见基本操作总结

    2023-08-08 16:56:09
  • Python开源自动化工具Playwright安装及介绍使用

    2023-08-20 13:17:43
  • 浅谈Python编程中3个常用的数据结构和算法

    2022-02-11 20:15:04
  • python 通过视频url获取视频的宽高方式

    2022-06-04 08:10:58
  • ASP日期格式化函数

    2010-08-08 19:18:00
  • vue3中cookie的详细使用过程

    2024-04-30 08:45:05
  • Python实现二维有序数组查找的方法

    2021-04-15 21:31:38
  • python使用matplotlib:subplot绘制多个子图的示例

    2021-01-26 18:13:08
  • Python使用matplotlib模块绘制图像并设置标题与坐标轴等信息示例

    2022-11-08 14:50:22
  • pyqt5 禁止窗口最大化和禁止窗口拉伸的方法

    2022-02-13 08:15:54
  • 解决python写的windows服务不能启动的问题

    2023-01-21 04:10:38
  • SQL Server视图管理中的四个限制条件

    2009-03-06 14:24:00
  • Gradio机器学习模型快速部署工具quickstart

    2023-06-30 01:09:52
  • 前端使用svg图片改色实现示例

    2022-03-14 03:54:14
  • python 通过exifread读取照片信息

    2022-12-09 13:44:53
  • Python3.6.0+opencv3.3.0人脸检测示例

    2023-07-23 05:51:07
  • asp之家 网络编程 m.aspxhome.com