Jquery获取radio选中值实例总结

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=//www.jb51.net/kf/201110/'rd2']").attr('checked','true');
或者
$("input[value=//www.jb51.net/kf/201110/'rd2']").attr('checked','true');

6.删除Value值为rd2的Radio

$("input:radio[value=//www.jb51.net/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为选中值

(0)

相关推荐

  • jquery radio 动态控制选中失效问题的解决方法

    性别选择 男女 通常是使用radio 在修改客户资料时 需要根据信息对radio进行选中. 最初的代码做法是 console.log($(':radio[value='+data[2]+']').attr('checked')); 但测试后有问题 在分别选中男女以后 就会失效. 后来搜了资料 原来是jQuery的版本问题 不同版本中对attr做的了修改 **如果你的jQuery库的版本是1.6以下的,使用attr方法 如果你的jquery库的版本是1.6(含1.6)以上的,使用prop方法**

  • jQuery实现 RadioButton做必选校验功能

    关于这个问题,百度里说法纷纷,大都无用,或者说版本太老太罗嗦,这里介绍一个可借鉴的用法: 按理说,页面上的若干个Radio Button 在页面加载初始化时,是默认选中第一个的,但总是避免不了一些特殊的需求--所有的单选按钮都不选中,仅当用按钮触发事件时再去做必选校验.例如: <div class="col-xs-4 col-sm-4 col-md-4"> <div class="radio col-mr-20"> @Html.RadioBu

  • jQuery获取单选按钮radio选中值与去除所有radio选中状态的方法

    如下所示: 喜欢<input type="radio" name="likea" id="" value="3" /> 一般<input type="radio" name="likea" id="" value="2" /> 不喜欢<input type="radio" name="li

  • jQuery实现radio第一次点击选中第二次点击取消功能

    由于项目的需求,要求radio点击两次后为取消状态,不方便修改为checkbox,可以用正面的方法实现. // jquery $('input:radio').click(function(){ //alert(this.checked); // var $radio = $(this); // if this was previously checked if ($radio.data('waschecked') == true){ $radio.prop('checked', false);

  • JQuery判断radio单选框是否选中并获取值的方法

    一.设置选中方法 代码如下: $("input[name='名字']").get(0).checked=true; $("input[name='名字']").attr('checked','true'); $("input[name='名字']:eq(0)").attr("checked",'checked'); $("input[name='radio_name'][checked]").val();

  • layui中使用jquery控制radio选中事件的示例代码

    勾选 form.on('radio(yesHave)', function (data) { $("input[name='hasPrepaidVoucher'][title='有']").attr('checked',true); form.render(); }); 取消勾选 form.on('radio(noIssuer)', function (data) { debugger; $("input[name='voucherIssuer']").remove

  • Jquery获取radio选中的值

    话不多说,请看代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> new docum

  • Jquery获取radio选中值实例总结

    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('ch

  • jquery获取select选中值的方法分析

    本文实例讲述了jquery获取select选中值的方法.分享给大家供大家参考,具体如下: 误区: 以前一直以为jquery获取select中option被选中的文本值,是这样写的: 复制代码 代码如下: $("#s").text(); //获取所有option的文本值 实际上应该这样: 复制代码 代码如下: $("#s option:selected").text(); //获取选中的option的文本值 获取select中option的被选中的value值: $(

  • jQuery 获取select选中值及清除选中状态

    不会使用jQuery的后台真心伤不起,获取select下拉列表的值都让我搞了好一阵,然而并没有结束,恢复选中状态也是花了我半个小时. 这里先记一下省的以后会忘了. 获取jQuery中select下拉列表中的lang属性值: <span style="font-size:14px;"> var provinceSearch = $("#loc_province_search").find("option:selected").attr(

  • jquery获取select选中值的文本,并赋值给另一个输入框的方法

    html 代码: <select id="ReviewStatus" name="ReviewStatus"><option selected="selected" value="0">空号</option> <option value="1">不接</option> <option value="2">不需要<

  • jQuery获取radio选中项的值实例

    <title></title> <script src="js/jquery-1.7.2.min.js"></script> <script type="text/javascript"> $(function () { //没有默认选中的情况 //先判断radio是否有选中项,再获取选中的值 $("#btnclick").click(function () { //获取选中项的valu

  • JQuery控制Radio选中方法分析

    本文实例讲述了JQuery控制Radio选中方法.分享给大家供大家参考.具体如下: 方法1: $(function () { $("#spanNan").click(function () { $("#Radio1").attr("checked", true); $("#Radio2").attr("checked", false); }); $("#spanNv").click(fu

  • jquery获取radio值实例

    本文实例讲述了jquery获取radio值的方法,分享给大家供大家参考.具体如下: 单选组radio: 复制代码 代码如下: $("input[@type=radio][@checked]").val(); 单选组 radio: 复制代码 代码如下: $("input[@type=radio]").attr("checked",'2');//设置value=2的项目为当前选中项 获取一组radio被选中项的值 复制代码 代码如下: var ite

  • JS 如何获取radio选中后的值及不选择取radio的值

    以下是网上摘下的文章(未测试但很规范可以模仿) 复制代码 代码如下: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>text</title> <script> var chk = 0; window.onload=function (){ var chkObjs

随机推荐