jquery select取值,賦值操作


[導讀]  jquery select取值,賦值操作一、獲取Select獲取select 選中的 text : $(" ddlRegType") find("option:selected") text();獲取select選中的索引: $(" ddlRegType") get(0) selectedIndex;二、設置Select設

select" rel="nofollow">jquery select取值,賦值操作

一、獲取Select 

獲取select 選中的 text : 

$("#ddlRegType").find("option:selected").text();

 

獲取select選中的索引: 

$("#ddlRegType").get(0).selectedIndex;

 

二、設置Select

設置select 選中的索引: 

$("#ddlRegType").get(0).selectedIndex = index;//index為索引值

 

設置select 選中的value:

$("#ddlRegType").attr("value","Normal“);

$("#ddlRegType").val("Normal");

$("#ddlRegType").get(0).value = value;

 

設置select 選中的text:

 1 var count = $("#ddlRegType option").length;
 2 
 3 for(var i=0;i<count;i++)  
 4 {

 5   if($("#ddlRegType ").get(0).options[i].text == text)  
 6     {  
 7         $("#ddlRegType ").get(0).options[i].selected = true;  
 8         break;  
 9     }  
10 }

 

$("#select_id option[text='jQuery']").attr("selected", true);

 

設置select option項:

 $("#select_id").append("<option value='Value'>Text</option>");  //添加一項option

 $("#select_id").prepend("<option value='0'>請選擇</option>"); //在前面插入一項option

 $("#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

 

清空 Select:

$("#ddlRegType ").empty();


下拉框:

var cc1   = $(".formc select[@name='country'] option[@selected]").text(); //得到下拉菜單的選中項的文本(注意中間有空格)
var cc2 = $('.formc select[@name="country"]').val();   //得到下拉菜單的選中項的值
var cc3 = $('.formc select[@name="country"]').attr("id"); //得到下拉菜單的選中項的ID屬性值
$("#select").empty();//清空下拉框//$("#select").html('');
$("<option value='1'>1111</option>").appendTo("#select")//添加下拉框的option

稍微解釋一下:
1.select[@name='country'] option[@selected] 表示具有name 屬性,
並且該屬性值為'country' 的select元素 里面的具有selected 屬性的option 元素;
可以看出有@開頭的就表示后面跟的是屬性。

2,單選框:
$("input[@type=radio][@checked]").val();   //得到單選框的選中項的值(注意中間沒有空格)
$("input[@type=radio][@value=2]").attr("checked",'checked'); //設置單選框value=2的為選中狀態.(注意中間沒有空格)

3,復選框:
$("input[@type=checkbox][@checked]").val(); //得到復選框的選中的第一項的值
$("input[@type=checkbox][@checked]").each(function(){ //由於復選框一般選中的是多個,所以可以循環輸出
   alert($(this).val());
   });

$("#chk1").attr("checked",'');//不打勾
$("#chk2").attr("checked",true);//打勾
if($("#chk1").attr('checked')==undefined){} //判斷是否已經打勾

//遍歷option和添加、移除option
function changeShipMethod(shipping){
 var len = $("select[@name=ISHIPTYPE] option").length
 if(shipping.value != "CA"){
  $("select[@name=ISHIPTYPE] option").each(function(){
   if($(this).val() == 111){
    $(this).remove();
   }
  });
 }else{
  $("<option value='111'>UPS Ground</option>").appendTo($("select[@name=ISHIPTYPE]"));
 }
}


//取得下拉選單的選取值

$(#testSelect option:selected').text();
或$("#testSelect").find('option:selected').text();
或$("#testSelect").val();


免責聲明!

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



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