Syntax Explanation:
1. $ ("#select_id"). Change (function () {//code ...}); Adds an event for a select that fires when one of the items is selected
2. Var checktext=$ ("#select_id"). Find ("option:selected"). Text (); Gets the text selected by select
3. Var checkvalue=$ ("#select_id"). Val (); Gets the value of the Select selection
4. Var checkindex=$ ("#select_id"). Get (0). SelectedIndex; Get the index value selected by select
5. Var maxindex=$ ("#select_id option:last"). attr ("index"); Get the maximum index value for select
jquery Sets the Select selected text and value:
Syntax Explanation:
1. $ ("#select_id"). Get (0). selectedindex=1; To set the Select index value of 1
2. $ ("#select_id"). Val (4); Select to set the value of 4 for the Select
3. $ ("#select_id option[text= ' jQuery ']"). attr ("selected", true); Set the text value of select to the item selected for jquery
jquery Add/Remove option items for select:
Syntax Explanation:
1. $ ("#select_id"). Append ("<option value= ' value ' >Text</option>"); Append an option to select (dropdown)
2. $ ("#select_id"). Prepend ("<option value= ' 0 ' > Please select </option>"); Inserts an option for the Select (first position)
3. $ ("#select_id option:last"). Remove (); Delete index value maximum option (last) in select
4. $ ("#select_id option[index= ' 0 ']"). Remove (); Delete option with index value of 0 in select (first)
5. $ ("#select_id option[value= ' 3 ']"). Remove (); Delete option value= ' 3 ' in select
5. $ ("#select_id option[text= ' 4 ']"). Remove (); Delete option text= ' 4 ' in select
$ (' input[@name =items] '). Get (1). checked = true;3, check box:
$ ("input[@type =checkbox][@checked]"). Val (); Gets the value of the first item selected in the check box
$ ("input[@type =checkbox][@checked]"). each (function () {//, because the check box is typically selected for more than one, you can cycle the output
Alert ($ (this). Val ());
});
$ ("#chk1"). attr ("Checked", "");/no tick.
$ ("#chk2"). attr ("Checked", true);/tick
if ($ ("#chk1"). attr (' checked ') ==undefined) {}//judge whether it has been ticked
1.select[@name = ' country '] option[@selected] Represents the Name property,
and the attribute value is the option element with the selected attribute inside the Select element of ' country ';
It can be seen that a @ begins with a property that follows.
2, Radio Box:
$ ("input[@type =radio][@checked]"). Val (); Gets the value of the selected item in the Radio box (note that there are no spaces in the middle)
$ ("input[@type =radio][@value =2]"). attr ("Checked", ' checked '); Sets the value=2 of the radio box to the selected state. (Note that there are no spaces in between)
3, check box:
$ ("input[@type =checkbox][@checked]"). Val (); Gets the value of the first item selected in the check box
$ ("input[@type =checkbox][@checked]"). each (function () {//, because the check box is typically selected for more than one, you can cycle the output
Alert ($ (this). Val ());
});
$ ("#chk1"). attr ("Checked", "");/no tick.
$ ("#chk2"). attr ("Checked", true);/tick
if ($ ("#chk1"). attr (' checked ') ==undefined) {}//judge whether it has been ticked
In the end, let's summarize what we've described above
The code is as follows |
Copy Code |
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; } |