Home >Web Front-end >JS Tutorial >Summary of common methods for setting and obtaining Select
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='checkbox']").attr("checked",'true');//全选 }) $("#btn2").click(function(){ $("[name='checkbox']").removeAttr("checked");//取消全选 }) $("#btn3").click(function(){ $("[name='checkbox']:even").attr("checked",'true');//选中所有奇数 }) $("#btn4").click(function(){ $("[name='checkbox']").each(function(){//反选 if($(this).attr("checked")){ $(this).removeAttr("checked"); } else{ $(this).attr("checked",'true'); } }) }) $("#btn5").click(function(){//输出选中的值 var str=""; $("[name='checkbox'][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!