Jquery 获取 radio选中值

本文介绍了在Jquery 1.4版本以上如何操作Radio和DropDownList控件,包括获取选中值、设置选中状态、删除选项、遍历元素等常见操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

随着Jquery的作用越来越大,使用的朋友也越来越多。在Web中,由于CheckBox、Radiobutton 、DropDownList等控件使用的频率比较高,就关系到这些控件在Jquery中的操作问题。由于Jquery的版本更新很快,代码的写法也改变了许多,以下Jquery代码适query1.4版本以上。


Radio    


  



1.获取选中值,三种方法都可以:

$('input:radio:checked').val();

$("input[type='radio']:checked").val();

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

2.设置第一个Radio为选中值:

    $('input:radio:first').attr('checked', 'checked');

或者

$('input:radio:first').attr('checked', 'true');

注:attr("checked",'checked')= attr("checked", 'true')= attr("checked", true)

3.设置最后一个Radio为选中值:

$('input:radio:last').attr('checked', 'checked');

或者

$('input:radio:last').attr('checked', 'true');

4.根据索引值设置任意一个radio为选中值:

$('input:radio').eq(索引值).attr('checked', 'true');索引值=0,1,2....

或者

$('input:radio').slice(1,2).attr('checked', 'true');

5.根据Value值设置Radio为选中值

$("input:radio[value=http://www.2cto.com/kf/201110/'rd2']").attr('checked','true');

或者

$("input[value=http://www.2cto.com/kf/201110/'rd2']").attr('checked','true');

6.删除Value值为rd2的Radio

$("input:radio[value=http://www.2cto.com/kf/201110/'rd2']").remove();

7.删除第几个Radio

$("input:radio").eq(索引值).remove();索引值=0,1,2....

如删除第3个Radio:$("input:radio").eq(2).remove();

8.遍历Radio

$('input:radio').each(function(index,domEle){

     //写入代码

});



DropDownList















1.   获取选中项:

获取选中项的Value值:

      $('select#sel option:selected').val();

     或者

       $('select#sel').find('option:selected').val();

获取选中项的Text值:

      $('select#seloption:selected').text();

     或者

       $('select#sel').find('option:selected').text();

2.   获取当前选中项的索引值:

$('select#sel').get(0).selectedIndex;

3.   获取当前option的最大索引值:

$('select#sel option:last').attr("index")

4.   获取DropdownList的长度:

$('select#sel')[0].options.length;

或者

$('select#sel').get(0).options.length;

5.  设置第一个option为选中值:

$('select#sel option:first').attr('selected','true')

或者

 $('select#sel')[0].selectedIndex = 0;

6.   设置最后一个option为选中值:

单选按钮的点击事件可以通过jQuery来实现。首先,在HTML代码中给每个单选按钮添加一个相同的name属性,然后在jQuery的初始化函数内,使用change()方法监听单选按钮的变化事件。当单选按钮的发生变化时,change()方法会触发一个回调函数。在这个回调函数中,可以使用条件语句来判断当前选中的单选按钮的,并执行相应的操作。 例如,下面是一个示例的HTML代码和jQuery代码: HTML代码: ```html<td width="15%" class="td_title">按设备或医生</td> <td> <input type="radio" id="sMachineCode" name="choice" value='1' checked="checked"/>设备编号&nbsp; <input type='radio' id='visitDoc' name='choice' value='2' >随访医生&nbsp; <input type='text' id='smachined' name='sMachineCode' placeholder="请输入设备编号" value='' > <input type='text' id='doctor' name='visitDoc' style="display:none" placeholder="请输入医生姓名" value='' > </td> ``` jQuery代码: ```javascript$(function() { $('input:radio[name="choice"]').change(function() { var v = $(this).val(); if (v == "2") { $("#doctor").show(); $("#smachined").hide(); } else { $("#doctor").hide(); $("#smachined").show(); } }); }); ``` 在这个例子中,当选择“随访医生”的单选按钮时,医生姓名输入框会显示出来,而设备编号输入框会隐藏起来。当选择“设备编号”的单选按钮时,医生姓名输入框会隐藏起来,设备编号输入框会显示出来。这些操作都是通过change()方法的回调函数实现的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值