JQuery Select control Plug-in _jquery

Source: Internet
Author: User
Share the code with you:
JavaScript code
Copy Code code as follows:

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;
}

When used, first introduce the Jquery.js file, then introduce the Jquery.liu.select.js file, and then call the plug-in's method. For example, I want to clear all the items in the dropdown box with ID selemail, so I can do this: $ ("#selEmail"). ClearAll ();
Description: The method in the plug-in in IE7 and Firefox validated through, there are errors and need to improve the place also hope that everyone criticized.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.