Jquery操作Select集錦

來源:互聯網
上載者:User
//得到select項的個數  
jQuery.fn.size = function(){  
    return jQuery(this).get(0).options.length;  
}  

//獲得選中項的索引  
jQuery.fn.getSelectedIndex = function(){  
    return jQuery(this).get(0).selectedIndex;  
}  

//獲得當前選中項的文本  
jQuery.fn.getSelectedText = function(){  
    if(this.size() == 0) return "下拉框中無選項";  
    else{  
        var index = this.getSelectedIndex();        
        return jQuery(this).get(0).options[index].text;  
    }  
}  

//獲得當前選中項的值  
jQuery.fn.getSelectedValue = function(){  
    if(this.size() == 0)   
        return "下拉框中無選中值";  
      
    else
        return jQuery(this).val();  
}  

//設定select中值為value的項為選中  
jQuery.fn.setSelectedValue = function(value){  
    jQuery(this).get(0).value = value;  
}  

//設定select中文本為text的第一項被選中  
jQuery.fn.setSelectedText = function(text)  
{  
    var isExist = false;  
    var count = this.size();  
    for(var i=0;i<count;i++)  
    {  
        if(jQuery(this).get(0).options[i].text == text)  
        {  
            jQuery(this).get(0).options[i].selected = true;  
            isExist = true;  
            break;  
        }  
    }  
    if(!isExist)  
    {  
        alert("下拉框中不存在該項");  
    }  
}  
//設定選中指定索引項目  
jQuery.fn.setSelectedIndex = function(index)  
{  
    var count = this.size();      
    if(index >= count || index < 0)  
    {  
        alert("選中項索引超出範圍");  
    }  
    else
    {  
        jQuery(this).get(0).selectedIndex = index;  
    }  
}  
//判斷select項中是否存在值為value的項  
jQuery.fn.isExistItem = function(value)  
{  
    var isExist = false;  
    var count = this.size();  
    for(var i=0;i<count;i++)  
    {  
        if(jQuery(this).get(0).options[i].value == value)  
        {  
            isExist = true;  
            break;  
        }  
    }  
    return isExist;  
}  
//向select中添加一項,顯示內容為text,值為value,如果該項值已存在,則提示  
jQuery.fn.addOption = function(text,value)  
{  
    if(this.isExistItem(value))  
    {  
        alert("待添加項的值已存在");  
    }  
    else
    {  
        jQuery(this).get(0).options.add(new Option(text,value));  
    }  
}  
//刪除select中值為value的項,如果該項不存在,則提示  
jQuery.fn.removeItem = function(value)  
{      
    if(this.isExistItem(value))  
    {  
        var count = this.size();          
        for(var i=0;i<count;i++)  
        {  
            if(jQuery(this).get(0).options[i].value == value)  
            {  
                jQuery(this).get(0).remove(i);  
                break;  
            }  
        }          
    }  
    else
    {  
        alert("待刪除的項不存在!");  
    }  
}  
//刪除select中指定索引的項  
jQuery.fn.removeIndex = function(index)  
{  
    var count = this.size();  
    if(index >= count || index < 0)  
    {  
        alert("待刪除項索引超出範圍");  
    }  
    else
    {  
        jQuery(this).get(0).remove(index);  
    }  
}  
//刪除select中選定的項  
jQuery.fn.removeSelected = function()  
{  
    var index = this.getSelectedIndex();  
    this.removeIndex(index);  
}  
//清除select中的所有項  
jQuery.fn.clearAll = function()  
{  
    jQuery(this).get(0).options.length = 0;  
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.