欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

jquery单选框radio绑定click事件实现和是否选中的方法

发布时间:2025/3/21 54 豆豆
生活随笔 收集整理的这篇文章主要介绍了 jquery单选框radio绑定click事件实现和是否选中的方法 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

使用jquery获取radio的值,最重要的是掌握jquery选择器的使用,在一个表单中我们通常是要获取被选中的那个radio项的值,所以要加checked来筛选,比如有以下的一些radio项:

1.<input type="radio" name="testradio" value="jquery获取radio的值" />jquery获取radio的值
2.<input type="radio" name="testradio" value="jquery获取checkbox的值" />jquery获取checkbox的值
3.<input type="radio" name="testradio" value="jquery获取select的值" />jquery获取select的值
要想获取某个radio的值有以下的几种方法,直接给出代码:

1、$('input[name="testradio"]:checked').val();
2$('input:radio:checked').val();
3$('input[@name="testradio"][checked]');
4$('input[name="testradio"]').filter(':checked');

差不多挺全的了,如果我们要遍历name为testradio的所有radio呢,代码如下
$('input[name="testradio"]').each(function(){alert(this.value);});

如果要取具体某个radio的值,比如第二个radio的值,这样写
$('input[name="testradio"]:eq(1)').val()

 

下面实例讲述了jquery单选框radio绑定click事件实现方法。分享给大家供大家参考。

具体实现方法如下:

<html> <head> <title>单选框radio绑定click事件</title> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <script type="text/javascript" src="jquery-1.8.2.min.js"></script> <script type="text/javascript">$(function(){$(":radio").click(function(){alert("您是..." + $(this).val());});}); </script> <style type="text/css"> h5{color:blue;} textarea{resize:none;} #word{color:red;} </style> </head> <body> <h5>单选框 radio 绑定 click 事件</h5> <form action="" method=""><input type="radio" name="gender" value="男性">男性<input type="radio" name="gender" value="女性">女性<input type="radio" name="gender" value="春哥">春哥 </form> </body> </html>

 

转载于:https://www.cnblogs.com/shenzikun1314/p/6843364.html

《新程序员》:云原生和全面数字化实践50位技术专家共同创作,文字、视频、音频交互阅读

总结

以上是生活随笔为你收集整理的jquery单选框radio绑定click事件实现和是否选中的方法的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。