asp.net下拉框(select)的基本操作


1、獲取單個select框的選中值。
   js方法獲取:
    獲取value值var value = document.getElementById("select_ID").value;
    獲取text值:var selectObj= document.getElementById("select_ID");
                  var text=selectObj[selectObj.selectedIndex].innerHTML;
   jquery方法獲取:
    獲取value值:$("#select_ID").val();
    獲取text值:$("#select_ID").find("option:selected").text();

2、獲取所有select框的選中值
      var selectList = document.getElementsByTagName("select_Name");
    for (var i = 0; i < selectList.length; i++) {
              alert(selectList[i].value);//獲取value值
       alert($(selectList[i]).find("option:selected").text());//獲取text值

       }
3、為select框添加下拉項
  js方法:
   var selectObj= document.getElementById("select_ID");
   var opp = new Option(text, value);
   selectObj.add(opp);
  jquery方法:
   $("#select_ID").append("<option value='Value'>Text</option>");  //添加一項option
   
$("#select_ID").prepend("<option value='0'>請選擇</option>"); //在前面插入一項option
4、移除select框下拉項
  
批量刪除:
   var selectObj = document.getElementById("select_ID");
         var optionItems = selectObj.options;
   for (var i = optionItems.length-1; i >=0 ; i--) {
                $(optionItems[i]).remove();
        }
  單個刪除:    
    $("#select_ID option:last").remove(); //刪除索引值最大的Option
    $("#select_ID option[index='0']").remove();//刪除索引值為0的Option
    $("#select_ID option[value='3']").remove(); //刪除值為3的Option
    $("#select_ID option[text='4']").remove(); //刪除text值為4的Option
5、設置select的值
  設置select 選中的value:
    $("#select_ID").attr("value","Normal“);
    $("#select_ID").val("Normal");
    $("#select_ID").get(0).value = value;
    document.
  設置select 選中的text:
    var count=$("#select_ID option").length;//select下拉框的option條數
    for(var i=0;i<count;i++)  {          
      if($("#select_ID").get(0).options[i].text == “text”)         
      {             
        $("#select_ID").get(0).options[i].selected = true;                       
        break;         
      }     
    }
  設置select 選中的索引:
       $("#select_ID").get(0).selectedIndex=index;//index為索引值

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM