【转】浅析JQuery获取和设置Select选项的常用方法总结

1.获取select 选中的 text:
 $("#cusChildTypeId").find("option:selected").text();
 $("#cusChildTypeId option:selected").text()

2.获取select选中的 value:
 $("#ddlRegType ").val();

3.获取select选中的索引:
      $("#ddlRegType ").get(0).selectedIndex;

4.得到select项的个数  
 $("#cusChildTypeId").get(0).options.length

5.设置select 选中的索引:
     $("#cusChildTypeId").get(0).selectedIndex=index;//index为索引值

6.设置select 选中的value:
    $("#cusChildTypeId").attr("value","Normal");
    $("#cusChildTypeId").val("Normal");
    $("#cusChildTypeId").get(0).value = "Normal";

7.设置select 选中的text:
 1>.var count=$("#cusChildTypeId").get(0).options.length;
     for(var i=0;i<count;i++)
         {          
  if($("#cusChildTypeId").get(0).options.text == text)
         {
             $("#cusChildTypeId").get(0).options.selected = true;
             break;
         }
        }

2>.$("#cusChildTypeId").val(text);
    $("#cusChildTypeId").change();

8.向select中添加一项,显示内容为text,值为value  
 $("#cusChildTypeId").get(0).options.add(new Option(text,value));

9.删除select中值为value的项
        var count = $("#cusChildTypeId").size();          
        for(var i=0;i<count;i++)  
        {  
            if($("#cusChildTypeId").get(0).options[i].value == value)  
            {  
                $("#cusChildTypeId").get(0).remove(i);  
                break;  
            }
        }

10.清空 Select:
 1>. $("#cusChildTypeId").empty();
 2>. $("#cusChildTypeId").get(0).options.length = 0;

转自:http://blog.csdn.net/lovelyhermione/article/details/4526377

时间: 2024-07-29 07:22:38

【转】浅析JQuery获取和设置Select选项的常用方法总结的相关文章

JQuery获取和设置Select选项的常用方法总结

1.获取select 选中的 text: $("#cusChildTypeId").find("option:selected").text(); $("#cusChildTypeId option:selected").text() 2.获取select选中的 value: $("#ddlRegType ").val(); 3.获取select选中的索引: $("#ddlRegType ").get(0)

JQuery获取和设置Select选项常用方法总结 (转)

1.获取select 选中的 text: $("#cusChildTypeId").find("option:selected").text(); $("#cusChildTypeId option:selected").text()2.获取select选中的 value: $("#ddlRegType ").val();3.获取select选中的索引:      $("#ddlRegType ").get

jquery 获取和设置Select选项常用方法总结

1.获取select 选中的 text:$("#cusChildTypeId").find("option:selected").text();$("#cusChildTypeId option:selected").text()2.获取select选中的 value:$("#ddlRegType ").val();3.获取select选中的索引:       $("#ddlRegType ").get(0

JQuery获取和设置Select选项常用方法总结

1.获取select 选中的 text: $("#cusChildTypeId").find("option:selected").text(); $("#cusChildTypeId option:selected").text()2.获取select选中的 value: $("#ddlRegType ").val();3.获取select选中的索引:      $("#ddlRegType ").get

jquery 获取和设置 select下拉框的值

<div style="display:block;text-align:left;margin-left:1em;"> <select id="uploadFrequency" style="width:50%;font-size:1em;" onchange="changeFunction()"> <option value="-1" selected="sele

jquery获取和设置radio,check,select选项

select控件选项 1,获取select选中的value值 $("#selectID").val(); 2,获取select选中的text的值 $("#selectID").find("option:selected").text() 3,设置select的第几项为当前选中项 $("#selectID").attr("value",2);//设置第二项为当前选中项 4,添加option $("s

jQuery -&gt; 获取/设置/删除DOM元素的属性

Sum square difference Problem 6 The sum of the squares of the first ten natural numbers is, 12 + 22 + ... + 102 = 385 The square of the sum of the first ten natural numbers is, (1 + 2 + ... + 10)2 = 552 = 3025 Hence the difference between the sum of

jQuery -&gt; 获取/设置HTML或TEXT内容

jQuery提供了两个API可以直接用来为元素添加内容. html() text() 其中html()是为指定的元素添加html内容 text()是为指定的元素添加文本内容 两者的区别在于,text中的内容是纯文本,不会被解析为html 如果要对如下html代码进行操作 <body> <p></p> </body> 使用html() $('p').html('<strong>Hello World</strong>, I am a &

Jquery获取html中select,radiobutton选中的值写法

1.Html代码: <select name="" class="qixian" id="tbCheckCycleUnit"> <option value="1">天</option> <option value="2">月</option> <option value="3">年</option> &l