国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

jQuery實現select下拉框獲取當前選中文本、值、索引

2024-05-06 15:17:28
字體:
來源:轉載
供稿:網友

話不多說,請看代碼:

//直接保存后綴.htnl用谷歌瀏覽器打開,親測有效<head><script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script><script> $(function(){  //為Select添加事件,當選擇其中一項時觸發  $("select:eq(0)").change(function(){   //code  });  //獲取Select選中的Text:結果是由所有匹配元素包含的文本內容組合起來的文本  var checkText = $("select:eq(0) :selected").text();//建議用這個簡單     = $("select:eq(0) option:selected").tetx();     = $("#One").find(":selected").text();     = $("#One").find("option:selected").text(); //如果多選,將返回一個數組,其包含所選的值。  var checkValue = $("#select_id").val(); //獲取Select選中匹配元素的當前值,即[即使多選也只]取得第一個匹配元素的val內容  var checkValue = $("select:eq(0) :selected").val();//=========強烈建議用這個,以防多選  //獲取Select選中的索引值  var checkIndex = $("#select_id ").get(0).selectedIndex;   //獲取Select最大的索引值   var maxIndex = $("#select_id :last").prop("index"); //建議用這個     = $("#select_id option:last").prop("index");      = $("select:eq(0)").find(":last").prop("index")     = $("select:eq(0)").find("option:last").prop("index") //========================================================================================= //jQuery設置Select選擇的 Text和Value: // 設置Select的Value值為4的項選中 $("#select_id ").val(4); //用這個  $("#select_id [value='4']").prop("selected", true); $("#select_id option[value='4']").prop("selected", true); //設置select中的第一個option被選中 $("select :first").prop("selected", true);//這個 $("select :first").prop("selected", 'selected');  $("select option:first").prop("selected", "true");  $("select option:first").prop("selected", "selected");  //============================================================================================ //jQuery添加/刪除Select的Option項 $("#select_id").append("<option value='Value'>Text</option>"); //為Select末尾追加一個Option(下拉項) $("#select_id").prepend("<option value='0'>請選擇</option>"); //為Select首部插入一個Option(第一個位置) $("#select_id :last").remove(); //刪除Select中索引值最大Option(最后一個) $("#select_id :fist").remove(); //刪除Select中索引值最小為0Option(第一個) $("#select_id [value='3']").remove(); //刪除Select中Value='3'的Option }); </script></head><table> <tr>   <td>    <!--multiple設定下拉框可以多選,size設定下拉框不呈現下拉方式,-->    <select size="12" id="One" multiple="multiple">     <option value='1'>蘋果</option>     <option value="2">香蕉</option>     <option value="3">草莓</option>     <option value="4">橘子</option>    </select>   </td>   <td>     <input type="button" value=">>>"><br>     <input type="button" value=" > "><br>     <input type="button" value=" < "><br>     <input type="button" value="<<<"><br>   </td>   <td>     <select size="12" id="two" multiple="multiple">      <option value="5">葡萄</option>     </select>   </td>   <td>     <input type="button" value=" up "><br><br>     <input type="button" value="down"><br>   </td> </tr></table>            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 黑河市| 海晏县| 尼勒克县| 金塔县| 文昌市| 平安县| 拉萨市| 淮安市| 铅山县| 承德市| 呼伦贝尔市| 兰西县| 营口市| 通山县| 嘉定区| 巴彦淖尔市| 璧山县| 时尚| 东兰县| 文成县| 雅江县| 本溪市| 铜鼓县| 乌苏市| 兰西县| 河津市| 长子县| 天祝| 乌鲁木齐县| 仪陇县| 电白县| 临沂市| 盘山县| 雷波县| 徐州市| 凤台县| 鄂尔多斯市| 新巴尔虎右旗| 澳门| 万州区| 贡觉县|