欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jquery操作select常見方法大全【7種情況】

 更新時間:2019年05月28日 10:35:30   作者:輕舞肥羊  
這篇文章主要介紹了jquery操作select常見方法,結(jié)合實例形式總結(jié)分析了jQuery操作select常見的7種情況與相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了jquery操作select常見方法。分享給大家供大家參考,具體如下:

在前段HTML頁面設(shè)計中select 下拉框,或者 在 multiple="multiple" 時,表現(xiàn)為列表。經(jīng)常會在頁面上對其進行操作,這些操作不外乎:

1. 得到選中的 select 的 option 的值或者text.
2. 刪除選中的 select 的 option.
3. 向select中增加新的option.
4. 得到select option 長度,也就是個數(shù)size
5. 清空select.
6. 兩個select 框之間互相添加刪除,從左邊到右邊,從右邊到左邊的操作,通常是多選情況。
7. 判斷在 select 框中是否存在某一個值的選項

對第一種情況,用如下方法:

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

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

對第二種情況,刪除的處理:

$("#select_id option:last").remove();  //刪除Select中索引值最大Option(最后一個)
$("#select_id option[index='0']").remove();  //刪除Select中索引值為0的Option(第一個)
$("#select_id option[value='3']").remove();  //刪除Select中Value='3'的Option
$("#select_id option[text='4']").remove();  //刪除Select中Text='4'的Option

如果要刪除選中的option ,則需要先得到 選中option 的序號. var checkIndex=$("#select_id ").get(0).selectedIndex; 然后再調(diào)用上面的方法刪除.

對第三種情況,增加option 的處理:

$("#select_id").append("<option value='Value'>Text</option>");  //為Select追加一個Option(下拉項)
$("#select_id").prepend("<option value='0'>請選擇</option>");  //為Select插入一個Option(第一個位置)

對第四種情況,得到select 的長度

var totalcount=$("#single_user_choice").get(0).options.length;

第五種情況,清空select

$("#single_user_choice").get(0).options.length=0;

第六種情況。兩個select 框之間互相添加刪除,從左邊到右邊,從右邊到左邊的操作,通常是多選情況,也就是設(shè)置了 multiple="multiple" 。

var $options = $('#select1 option:selected');//獲取當前選中的項
var $remove = $options.remove();//刪除下拉列表中選中的項
$remove.appendTo('#select2');//追加給對方

第七種情況,判斷在select 是否存在某個value  的 option

function is_Exists(selectid,value){
  var theid='#'+selectid;
  var count=$(theid).get(0).options.length;
  var isExist = false;
  for(var i=0;i<count;i++){
    if ($(theid).get(0).options[i].value == value){
      isExist=true;
      break;
    }
  }
  return isExist;
}

更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery常見事件用法與技巧總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jQuery操作json數(shù)據(jù)技巧匯總》、《jQuery擴展技巧總結(jié)》、《jQuery常見經(jīng)典特效匯總》及《jquery選擇器用法總結(jié)

希望本文所述對大家jQuery程序設(shè)計有所幫助。

相關(guān)文章

最新評論