jQuery获取选中单选按钮radio的值

原创 2017-01-13 15:22:03 382
摘要: 实例1:<div id="wrap">   <input type="radio" name="payMethod" value="1" />男   <input type=&q

 实例1:

<div id="wrap">
  <input type="radio" name="payMethod" value="1" />男
  <input type="radio" name="payMethod" value="2" />女
</div>

  获取一组单选按钮对象:var obj_payPlatform = $('#wrap input[name="payMethod"]');

  获取被选中按钮的值 :var val_payPlatform = $('#wrap input[name="payMethod"]:checked ').val();

实例2:

使用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的值有以下的几种方法,直接给出代码:

$('input[name="testradio"]:checked').val();
$('input:radio:checked').val();
$('input[@name="testradio"][checked]');
$('input[name="testradio"]').filter(':checked');

差不多挺全的了,如果我们要遍历name为testradio的所有radio呢,代码如下

$('input[name="testradio"]').each(function(){2.alert(this.value);3.});

如果要取具体某个radio的值,比如第二个radio的值,这样写

$('input[name="testradio"]:eq(1)').val()

更多关于jQuery获取选中单选按钮radio的值请关注PHP中文网(www.php.cn)其他文章!   


发布手记

热门词条