閱讀735 返回首頁    go 阿裏雲 go 技術社區[雲棲]


JavaScript:Select標簽

jQuery獲取Select選擇的Text和Value:
1. $("#select_id").change(function(){//code...});   //為Select添加事件,當選擇其中一項時觸發
2. var checkText=$("#select_id").find("option:selected").text();  //獲取Select選擇的Text
3. var checkValue=$("#select_id").val();  //獲取Select選擇的Value
4. var checkIndex=$("#select_id ").get(0).selectedIndex;  //獲取Select選擇的索引值
5. var maxIndex=$("#select_id option:last").attr("index");  //獲取Select最大的索引值 

jQuery設置Select選擇的Text和Value:

1. $("#select_id ").get(0).selectedIndex=1;  //設置Select索引值為1的項選中
2. $("#select_id ").val(4);   //設置Select的Value值為4的項選中
3. $("#select_id option[text='jQuery']").attr("selected", true);   //設置Select的Text值為jQuery的項選中

jQuery添加/刪除Select的Option項:

1. $("#select_id").append("<option value='Value'>Text</option>");  //為Select追加一個Option(下拉項)
2. $("#select_id").prepend("<option value='0'>請選擇</option>");  //為Select插入一個Option(第一個位置)
3. $("#select_id option:last").remove();  //刪除Select中索引值最大Option(最後一個)
4. $("#select_id option[index='0']").remove();  //刪除Select中索引值為0的Option(第一個)
5. $("#select_id option[value='3']").remove();  //刪除Select中Value='3'的Option
5. $("#select_id option[text='4']").remove();  //刪除Select中Text='4'的Option

<h3>請選擇要前往的網站:</h3>
<select >
<option value="https://www.baidu.com">1.百度</option>
<option value="https://t.qq.com/qingshan008/"> 2.清山微博</option>
<option value="https://blog.csdn.net/a497785609"> 3.清山博客</option>
<option value="https://www.powereasy.net/"> 4.動易網絡</option>
</select>
<script language="Javascript">
//在網頁末尾添加以下js代碼:
var s=document.getElementById("gotopage");
if(s){
   s.attachEvent('onchange',mychange);//動態添加onchange事件,其他事件隻需改成事件名稱即可。
   function mychange(){
       window.location=(s.options[s.selectedIndex].value);//訪問所選option指定的網址,僅對IE有效。
   }
}
</script>

最後更新:2017-04-02 06:51:22

  上一篇:go iphone圖標資源總匯
  下一篇:go JAVA核心層--反射--動態代理