Home  >  Article  >  php教程  >  Summary of jQuery drop-down menu operations

Summary of jQuery drop-down menu operations

高洛峰
高洛峰Original
2016-12-15 16:16:381196browse

Get Select:

Get selected text:

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



Get selected value:

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



Get the selected index of select:

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



Set select:

Set the selected index of select :

$ ("#ddlregtype") .get (0) .selectEdindex = Index; // Index was the Value selected by SELECT:

$ ("#ddlregtype") .attr ("Value ","Normal");

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

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



Set select Select text:

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

for(var i=0;i                                                                                                           .options[i].text == text)
                                                                                                          
}



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



Set the select option item:



$("#select_id").append(""); //Add an option

$("#select_id").prepend("

jQuery.fn.size = function()
{
{ return jQuery(this).get(0).options.length;
}
//Get the index of the selected item
jQuery.fn.getSelectedIndex = function()
{ J Return jquery (this) .get (0) .selectEdindex;
}
// Get the current selected text
jquery.fn.getSelectExt = function () {
if (this.size () == 0)
)
{     
        return "下拉框中无选项";     
    }     
    else    
    {     
        var index = this.getSelectedIndex();           
        return jQuery(this).get(0).options[index].text;     
    }     
}
//Get the value of the currently selected item
jQuery.fn.getSelectedValue = function()
{
if(this.size() == 0)
{ return "No selected value in the drop-down box";
}
else
{
jQuery(this).get (0).value = value;
}
//Set the first item in the select as text to be selected
jQuery.fn.setSelectedText = function(text)
{
var isExist = false;
var count = this. size();
for(var i=0;i { if(jQuery(this).get(0).options[i].text == text)
{ jQuery(this) .get(0).options[i].selected = true;

{ }
}
//Set the selected index item
jQuery.fn.setSelectedIndex = function(index)
{
var count = this.size();
if(index >= count || index < 0)
{T Alert ("Selecting the index exceeds the range"); Item
jQuery.fn.isExistItem = function(value)
{
var isExist = false;
var count = this.size();
for(var i=0;i { if(jQuery(this).get(0).options[i].value == value)
{
             isExist = true;                                                                                                                                           is text, the value is value, if the value already exists, it will prompt
jQuery.fn .addOption = function(text,value)                                                                                                                        {                                 jQuery(this).get (0).options.add(new Option(text,value));
}
}
//Delete the item whose value is value in the select. If the item does not exist, it will prompt
jQuery.fn.removeItem = function( value)
{
if(this.isExistItem(value))
{ var count = this.size();
for(var i=0;i                                                                                                                                 get(0).options[i].value == value)                                                                                                                                                                                                                                                                                                                                                           The item to be deleted does not exist!");
}
}
//Delete the item with the specified index in select
jQuery.fn.removeIndex = function(index)
{
var count = this.size();
if( index > .remove(index);
}}} // Delete the selected item
jquery.fn.removeSelect = Function () {
var index = this.getSelectEdex (); Le // Clear Select All items in
jQuery.fn.clearAll = function()
{
Drop down menu operations To summarize related articles, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn