Home >Web Front-end >JS Tutorial >Summary of common methods for setting and obtaining Select

Summary of common methods for setting and obtaining Select

伊谢尔伦
伊谢尔伦Original
2017-06-19 16:16:431641browse

1. Get the text selected by select:
$("#cusChildTypeId").find("option:selected").text();
$("#cusChildTypeId option:selected").text ()

2. Get the value selected by select:
$("#ddlRegType ").val();

3. Get the index selected by select :
$("#ddlRegType ").get(0).selectedIndex;

4. Get the number of selected items
$("#cusChildTypeId").get(0).options .length

5.Set the selected index of select:
$("#cusChildTypeId").get(0).selectedIndex=index;//index is the index value

6. Set the selected value of select:
$("#cusChildTypeId").attr("value","Normal");
$("#cusChildTypeId").val("Normal");
$ ("#cusChildTypeId").get(0).value = "Normal";

7.Set select selected text:
1>.

var count=$("#cusChildTypeId").get(0).options.length;
 for(var i=0;i<count;i++)  
 {           
  if($("#cusChildTypeId").get(0).options.text == text)  
   {  
   $("#cusChildTypeId").get(0).options.selected = true;
   break;  
   }  
 }

2>.

$("#cusChildTypeId").val(text);
$("#cusChildTypeId").change();

8. Add an item to the select, the display content is text, the value is value
$("#cusChildTypeId").get(0).options.add(new Option(text,value)) ;

9.Delete items with value in select

var count = $("#cusChildTypeId").size();           
  for(var i=0;i<count;i++)   
  {   
     if($("#cusChildTypeId").get(0).options[i].value == value)   
     {   
       $("#cusChildTypeId").get(0).remove(i);   
        break;   
     }
  }

10. Clear Select:
1>. $("#cusChildTypeId").empty ();
2>. $("#cusChildTypeId").get(0).options.length = 0;

Example:

$("document").ready(function(){
$("#btn1").click(function(){
$("[name=&#39;checkbox&#39;]").attr("checked",&#39;true&#39;);//全选
})
$("#btn2").click(function(){
$("[name=&#39;checkbox&#39;]").removeAttr("checked");//取消全选
})
$("#btn3").click(function(){
$("[name=&#39;checkbox&#39;]:even").attr("checked",&#39;true&#39;);//选中所有奇数
})
$("#btn4").click(function(){
$("[name=&#39;checkbox&#39;]").each(function(){//反选
if($(this).attr("checked")){
$(this).removeAttr("checked");
}
else{
$(this).attr("checked",&#39;true&#39;);
}
})
})
$("#btn5").click(function(){//输出选中的值
var str="";
$("[name=&#39;checkbox&#39;][checked]").each(function(){
str+=$(this).val()+"\r\n";
//alert($(this).val());
})
alert(str);
})
})

The above is the detailed content of Summary of common methods for setting and obtaining Select. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn