jquery's selector is powerful, and I've been looking for it for a long time to add to the Select Options object.
Copy Code code as follows:
/**//*
FileName: jquery.liu.select.js
Function Description: This JS file for the jquery class library of a plug-in, the main implementation of the select operation.
Author: John Liu
Date of preparation: 2008/03/12
*/
Get the number of select items
JQuery.fn.size = function ()
{
Return JQuery (this). Get (0). Options.length;
}
Get the index of the selected item
JQuery.fn.getSelectedIndex = function ()
{
Return JQuery (this). Get (0). SelectedIndex;
}
Get the text of the currently selected item
JQuery.fn.getSelectedText = function ()
{
if (this.size () = = 0)
{
Return "No options in dropdown box";
}
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 dropdown box";
}
Else
{
Return JQuery (This). Val ();
}
}
Set the item in select that is value to be selected
JQuery.fn.setSelectedValue = function (value)
{
JQuery (this). Get (0). value = value;
}
Sets the first item with text in select selected
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 ("The item does not exist in the dropdown box");
}
}
Set Select the specified index entry
JQuery.fn.setSelectedIndex = function (index)
{
var count = This.size ();
if (Index >= count | | Index < 0)
{
Alert ("Checked index out of range");
}
Else
{
JQuery (this). Get (0). SelectedIndex = index;
}
}
To determine if an item in a select item has a value of
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;
}
Adds an item to the Select, displays the content as text, values is value, and prompts if the key value already exists
JQuery.fn.addOption = function (Text,value)
{
if (This.isexistitem (value))
{
Alert ("The value of the item being added already exists");
}
Else
{
JQuery (this). Get (0). Options.add (New Option (Text,value));
}
}
Deletes an item in the select that is value, and prompts if the item does not exist
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 ("The item to be deleted does not exist!");
}
}
Delete an item from a specified index in a select
JQuery.fn.removeIndex = function (index)
{
var count = This.size ();
if (Index >= count | | Index < 0)
{
Alert ("Index of items to be deleted is out of range");
}
Else
{
JQuery (this). Get (0). Remove (index);
}
}
Delete the selected item from the Select
jQuery.fn.removeSelected = function ()
{
var index = This.getselectedindex ();
This.removeindex (index);
}
Clear all items in a Select
JQuery.fn.clearAll = function ()
{
JQuery (this). Get (0). options.length = 0;
}