首頁  >  文章  >  web前端  >  jQuery實作select下拉方塊取得目前選取的文本

jQuery實作select下拉方塊取得目前選取的文本

小云云
小云云原創
2018-01-12 10:28:163371瀏覽

本文主要介紹了jQuery實作select下拉方塊取得目前選取文字、值、索引以及新增/刪除Select的Option項目的相關知識,具有很好的參考價值。下面跟著小編一起來看吧,希望能幫助大家。

話不多說,請看程式碼:

//直接保存后缀.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=&#39;4&#39;]").prop("selected", true);
 $("#select_id option[value=&#39;4&#39;]").prop("selected", true);
 //设置select中的第一个option被选中
 $("select :first").prop("selected", true);//这个
 $("select :first").prop("selected", &#39;selected&#39;); 
 $("select option:first").prop("selected", "true"); 
 $("select option:first").prop("selected", "selected"); 
 //============================================================================================
 //jQuery添加/删除Select的Option项
 $("#select_id").append("<option value=&#39;Value&#39;>Text</option>"); //为Select末尾追加一个Option(下拉项)
 $("#select_id").prepend("<option value=&#39;0&#39;>请选择</option>"); //为Select首部插入一个Option(第一个位置)
 $("#select_id :last").remove(); //删除Select中索引值最大Option(最后一个)
 $("#select_id :fist").remove(); //删除Select中索引值最小为0Option(第一个)
 $("#select_id [value=&#39;3&#39;]").remove(); //删除Select中Value=&#39;3&#39;的Option
 });
 </script>
</head>
<table>
 <tr>
   <td>
    <!--multiple设定下拉框可以多选,size设定下拉框不呈现下拉方式,-->
    <select size="12" id="One" multiple="multiple">
     <option value=&#39;1&#39;>苹果</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>

相關推薦:

#div+css模擬select下拉方塊實例程式碼

html中華支援分類Select下拉方塊的Optgroup標籤

#基於jQuery的select下拉方塊選擇觸發事件實例分析

以上是jQuery實作select下拉方塊取得目前選取的文本的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn