This includes jquery. Gets the number of select items, the text of the selected item before the index the first item of the value text of the selected item is selected to delete operations such as the selected item in the Select.
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 option 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;
}