jQuery取得select選擇的文本與值

來源:互聯網
上載者:User

標籤:網頁   evel   attr   cli   item   設定   getc   選擇   刪除索引   

 jQuery取得select選擇的文本與值

  1 jquery擷取select選擇的文本與值  2 擷取select :  3 擷取select 選中的 text :  4     $("#ddlregtype").find("option:selected").text();  5   6 擷取select選中的 value:  7     $("#ddlregtype ").val();  8   9 擷取select選中的索引: 10     $("#ddlregtype ").get(0).selectedindex; 11  12 設定select: 13 設定select 選中的索引: 14     $("#ddlregtype ").get(0).selectedindex=index;//index為索引值 15  16 設定select 選中的value: 17     $("#ddlregtype ").attr("value","normal“); 18     $("#ddlregtype ").val("normal"); 19     $("#ddlregtype ").get(0).value = value; 20  21 設定select 選中的text: 22  23     var count=$("#ddlregtype option").length; 24       for(var i=0;i<count;i++) 25          {           if($("#ddlregtype ").get(0).options[i].text == text) 26             { 27                 $("#ddlregtype ").get(0).options[i].selected = true; 28                 break; 29             } 30         } 31     $("#select_id option[text=‘jquery‘]").attr("selected", true); 32  33 設定select option項: 34  35     $("#select_id").append("<option value=‘value‘>text</option>");  //添加一項option 36     $("#select_id").prepend("<option value=‘0‘>請選擇</option>"); //在前面插入一項option 37     $("#select_id option:last").remove(); //刪除索引值最大的option 38     $("#select_id option[index=‘0‘]").remove();//刪除索引值為0的option 39     $("#select_id option[value=‘3‘]").remove(); //刪除值為3的option 40     $("#select_id option[text=‘4‘]").remove(); //刪除text值為4的option 41  42 清空 select: 43  44     $("#ddlregtype ").empty(); 45  46 工作需要,要獲得兩個表單中的值。 47  48 如何獲得從左邊選擇框添加到右邊選擇框中的值?我想了想用網頁特效可以獲得,這裡用了比較流行的jquery。 49 js代碼如下: 50  51     //擷取所有屬性值 var item = $("#select1").val(); 52     $(function(){ 53       $(‘#select1‘).each(  //獲得select1的所有值 54          function(){ 55             $(‘button‘).click(function(){ 56                 alert($(‘#select2‘).val());  //獲得select2中的select1值 57             }); 58          }); 59     }) 60     </script> 61  62 值得注意的是,不能直接寫成 63  64     $(function(){ 65       $(‘#select2‘).each(  //獲得select1的所有值,因為前面講選項從左邊添加到右邊,jquery其實並沒有真正將值從左邊傳到右邊。 66          function(){ 67             $(‘button‘).click(function(){ 68                 alert($(this).val());  //獲得select2中的select1值 69             }); 70          }); 71     }) 72  73 html: 74     <div class="centent"> 75             <select multiple="multiple" id="select1" name="dd" style="width:100px;height:160px;"> 76                 <option value="1">選項1</option> 77                 <option value="2">選項2</option> 78                 <option value="3">選項3</option> 79                 <option value="4">選項4</option> 80                 <option value="5">選項5</option> 81                 <option value="6">選項6</option> 82                 <option value="7">選項7</option> 83             </select> 84             <div> 85                 <span id="add" >選中添加到右邊&gt;&gt;</span> 86                 <span id="add_all" >全部添加到右邊&gt;&gt;</span> 87             </div> 88         </div> 89         <div class="centent"> 90             <select multiple="multiple" id="select2" name="sel" style="width: 100px;height:160px;"> 91             </select> 92             <div> 93                 <span id="remove">&lt;&lt;選中刪除到左邊</span> 94                 <span id="remove_all">&lt;&lt;全部刪除到左邊</span> 95             </div> 96         </div> 97  98 使用JQuery,Ajax調用動態填充Select的option選項 99 100     //綁定ClassLevel1單擊事件101         $("#ClassLevel1").change(function () {102             var id = $("#ClassLevel1").val();103             var level2 = $("#ClassLevel2");104             level2.empty();105             $("#ClassLevel3").hide();106             $.ajax({107                 url: "./askCommon.ashx?action=getclasslevel&pid=" + id,108                 data: { "type": "ajax" },109                 datatype: "json",110                 type: "get",111                 success: function (data) {112                     var json = eval_r(data);113                     for (var ind in json) {114                         level2.append($("<option value=‘" + json[ind].id + "‘>" + json[ind].typename + "</option>"));115                     }116     117                 }118             });119         })

 

jQuery取得select選擇的文本與值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.