首頁  >  文章  >  php教程  >  jQuery 下拉式選單操作總結

jQuery 下拉式選單操作總結

高洛峰
高洛峰原創
2016-12-15 16:16:381197瀏覽

取得Select :

 取得select 選取的text :

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

 
se🜠🎠🜠🎠🜵🜠 #ddlRegType ").val();

 

 取得select選取的索引:

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

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

 

 設定select 選取的value:Shval


 ","Normal“);

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

    $("#ddlRegType ").get(0).ue = valueue的text:

var count=$("#ddlRegType option").length;

  for(var i=0;i     .options[i].text == text)  
        {  
            ("#ddl
            break;  
        }  
    }

 

    }

 

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

 

設定select option項目:

 

 $("#select."). ='Value'>Text");  //新增一項option

 $("#select_id").prepend(""); / /在前面插入一項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();


jquery。值:

.val()

.text()


設定值 

.val('在這裡設定值')



$("readument"). ("#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").lick(btn5").lick( {//輸出選取的值 
var str=""; 
$("[name='checkbox'][checked]").each(function(){ 
str+=$(this).val()+" rn"; 
//alert($(this).val()); 
}) 
alert(str); 
}) 
}) 


================
}) 


================ ==================================================== ==

============================================== ====================

jQuery.fn.size = function()     
{     
    return jQuery(this).get(0).options.length ery.fn.getSelectedIndex = function()     
{
    return jQuery(this).get(0).selectedIndex;     
}     
//取得目前選取項目的文字       if(this.size() == 0)     
{     
        return "下拉方塊中無選項";     
    }     🠎    var index = this.getSelectedIndex();           
        return jQuery(this).get(0). 
//取得目前選取項目的值     
jQuery.fn.getSelectedValue = function()     
{          
        return "下拉方塊中無選取值";     
    }     
    else
    {     
        return jQuery(this).val();     
    }  
jQuery.fn.setSelectedValue = function(value)     
{     
    jQuery(this).get (0).value = value;     
}     
//設定為文字為text的第一項選取     
jQuery.fn.setSelect alse;     
    var count = this. size();     
    for(var i=0;i    {     
  
        {     
            jQuery(this) .get(0).options[i].selected = true;     
            isExist = true;         }     
    }     
    if(!isExist)     
   
}     
}     
//設定選取指定指數項     
jQuery.fn.setSelectedIndex = function(index)       
    if(index >= count || index     {     
alert("選取項目索引超出範圍");     
    }     
    else    
    {      
    }     
}     
//判斷select項是否有值為value項的項
jQuery.fn.isExistItem = function(value)     
{     
    var isExist = false;     
var count = this.size();     
    for(var i=0;i    {     
) .     
        {
            isExist = true;     
                 
    return isExist;     
}     
//向select中新增一項,顯示內容為text,且值為value,若該項值已存在,則提示  .addOption = function(text,value)     
{     
    if(this.isExistItem(value))          
    }     
    else    
    {     (0).options.add(new Option(text,value));     
    }     
}     
//刪除value)     
{         
    if(this.isExistItem(value))     
            
        for(var i=0;i    ). get(0).options[i].value == value)     
            {     
   }   
                                 
    }     
    else    
    {     
      {     待刪除的項目不存在!");     
    }     
}     
//使用select中指定索引
    var count = this.size();     
    if( index >= count || index     {     
        alert("待刪除項索引
    {     
        jQuery(this).get(0).remove(index);     
    }   }     
//刪除select中選定的項目     
jQuery.fn.removeSelected = function()     
{   this.removeIndex(index);     
}     
//清除select中的所有項目     
jQuery.fn.clearAll = function()     
{     
    jQuery(this).get(0).options.length = 00%)  




更多 jQuery 下拉式選單操作摘要相關文章請關注PHP中文網!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn