美文网首页
JQuery 获取 radio选中值

JQuery 获取 radio选中值

作者: 飞走的光年 | 来源:发表于2020-03-02 15:00 被阅读0次

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

一、input 标签

Jquery 获取 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){

     //写入代码

});
image.png

二、DropDownList

JQuery 获取 radio选中值

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;

相关文章

  • 业务开发中小技巧

    1.Myql统一新增基础字段: 2.Jquery获取radio选中值: 3.Jquery获取select选中值: ...

  • JQuery 获取 radio选中值

    随着JQuery的作用越来越大,使用的朋友也越来越多。在Web中,由于CheckBox、Radiobutton 、...

  • jQuery操作单选按钮(radio)用法

    转自:jQuery操作单选按钮(radio)用法 1.获取选中值,四种方法都可以: 2.设置第一个Radio为选中...

  • 【前端】jQuery获取radio、checkbox选中值

    作者:邹峰立,微博:zrunker,邮箱:zrunker@yahoo.com,微信公众号:书客创作,个人平台:ww...

  • 前端jquery操作记录

    1.jquery获取到页面指定的chekcbox的值 通过name获取选中值的方法: //jquery获取复选框值...

  • radio

    JQuery获取被选中的值:$(':radio[name="name"]:checked').val(); JQu...

  • jquery笔记

    jQuery获取一组radio的值 如题所示,方法如下:$("input[name='showCategory']...

  • 解决问题1

    11.jquery获取radio(表单中的单选按钮)的值 "identity":$("input[type='ra...

  • 使用Jquery获取Bootstrap Radio Group当

    例如有以下的bootstrap radio buttons group 现在,我需要使用Jquery来获取当前选中...

  • Jquery操作radio标签

    Jquery操作radio标签 HTML 监听radio事件,根据不同的值执行不同的业务逻辑代码$(":radio...

网友评论

      本文标题:JQuery 获取 radio选中值

      本文链接:https://www.haomeiwen.com/subject/uexvkhtx.html