Traversing option and adding, removing option
Syntax Explanation:
The code is as follows |
Copy Code |
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 |
Cases
The code is as follows |
Copy Code |
function Changeshipmethod (shipping) { var len = $ ("select[@name =ishiptype] option"). length if (shipping.value!= "CA") { $ ("select[@name =ishiptype] option"). each (function () { if ($ (this). val () = 111) { $ (this). Remove (); } }); }else{ $ ("<option value= ' >ups ground</option>"). Appendto ($ ("select[@name =ishiptype]"); } } |
The text and value selected by select:
$ (' #testSelect option:selected '). Text ();
Or
$ ("#testSelect"). Find (' option:selected '). Text ();
Or
$ ("#testSelect"). Val ();
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 a Select
Cases
jquery Gets the value of the Select, which is the value of the option's Value property that is selected
Pass
The code is as follows |
Copy Code |
ID of Select $ (' #select_id Option:selected '). Val (); $ (' #select_id '). Find (' option:selected '). Val (); Or in the native way. $ (' #select_id Option:selected ') [0].value; Pass Name of SELECT $ (' select[name= "Select_name"] Option:selected '). Val (); $ (' select[name= "Select_name"]). Find (' option:selected '). Val ();
|
Example 2
jquery Gets the selected option's InnerHTML value (that is, the text value, which is the content in the <option></option> Center).
code is as follows |
copy code |
//via The ID of select is $ (' #select_id option:selected '). Text (); $ (' #select_id '). Find (' option:selected '). Text () Or in the native way $ (' #select_id option:selected ') [0].innerhtml //Through Select's name $ (' select[name= ') Select_name "] option:selected '). Text (); $ (' select[name= "Select_name"]). Find (' option:selected '). text (); |