Rumah > hujung hadapan web > tutorial js > 设置获取Select常用方法汇总

设置获取Select常用方法汇总

伊谢尔伦
Lepaskan: 2017-06-19 16:16:43
asal
1709 orang telah melayarinya

1.获取select 选中的 text:
 $("#cusChildTypeId").find("option:selected").text();
 $("#cusChildTypeId option:selected").text()

2.获取select选中的 value:
 $("#ddlRegType ").val();

3.获取select选中的索引:
      $("#ddlRegType ").get(0).selectedIndex;

4.得到select项的个数   
 $("#cusChildTypeId").get(0).options.length

5.设置select 选中的索引:
     $("#cusChildTypeId").get(0).selectedIndex=index;//index为索引值

6.设置select 选中的value:
    $("#cusChildTypeId").attr("value","Normal");
    $("#cusChildTypeId").val("Normal");
    $("#cusChildTypeId").get(0).value = "Normal";

7.设置select 选中的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;  
   }  
 }
Salin selepas log masuk

2>.

$("#cusChildTypeId").val(text);
$("#cusChildTypeId").change();
Salin selepas log masuk

8.向select中添加一项,显示内容为text,值为value
$("#cusChildTypeId").get(0).options.add(new Option(text,value));

9.删除select中值为value的项

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;   
     }
  }
Salin selepas log masuk

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

实例:

$("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);
})
})
Salin selepas log masuk

Atas ialah kandungan terperinci 设置获取Select常用方法汇总. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan