码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2017-05-12 01:42:28      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:http   method   java   nbsp   16px   参考   实例   head   重要   

使用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>

 

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

标签:http   method   java   nbsp   16px   参考   实例   head   重要   

原文地址:http://www.cnblogs.com/shenzikun1314/p/6843364.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!