jquery Operation Select

Source: Internet
Author: User

When you use jquery to operate a Select, you often check the documentation and record the backup.

jquery Gets the text and value selected by select:

Syntax Explanation: 1. $ ("#select_id"). Change (function () {//code ...}); Adds an event for select, triggering 2 when one of the items is selected. var checktext=$ ("#select_id"). Find ("option:selected"). Text (); Gets the Select selected text 3. var checkvalue=$ ("#select_id"). Val (); Gets the value 4 selected by the Select. var checkindex=$ ("#select_id"). Get (0). SelectedIndex; Gets the index value of the select selection of 5. var maxindex=$ ("#select_id option:last"). attr ("index"); Gets the maximum index value of the Select jquery setting Select the text and value: syntax Explanation: 1. $ ("#select_id"). Get (0). selectedindex=1; Set the Select index value to 1 to select 2. $ ("#select_id"). Val (4); Set the value of select to 4 to 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 for select:  syntax explanation:  1. $ ("#select_id"). Append ("<option value= ' value ' >Text</option>"); Append an option to the Select (drop-down) 2. $ ("#select_id"). Prepend ("<option value= ' 0 ' > Please select </option>"); Inserts an option (first position)  3 for SELECT. $ ("#select_id option:last"). Remove (); Delete the index value in select maximum option (last)  4. $ ("#select_id option[index= ' 0 ']"). Remove (); Delete option (first)  5 with index value 0 in select. $ ("#select_id option[value= ' 3 ']"). Remove (); Delete the option 5 of value= ' 3 ' in select. $ ("#select_id option[text= ' 4 ']"). Remove (); Delete the option   //of text= ' 4 ' in select to traverse the option and add, remove option function changeshipmethod (shipping) {  var len = $ ("select[@name =ishiptype] option"). LENGTH&NBSP;IF (Shipping.value! = "CA") { $ ("select[@name = Ishiptype] option). each (function () { if ($ (). val () = = 111) { $ (this). Remove ();  } });  } else{ www.2cto.com$ ("<option value= ' 111 ' >ups ground</option>"). AppendTo ($ ("select[@name = Ishiptype]);  } }  //iterates over a set of select and sets the selected value $ ("Div:first select"). each (function () {$ ("Div:first select option[value= ' 0 ') "). attr (" selected ", True);});  //Gets the selected value of the dropdown menu   $ (#testSelect option:selected '). Text ();  or $ ("#testSelect"). Find (' option: Selected '). Text ();  or $ ("#testSelect"). Val ();

Clear Select
$ (". Selector2"). empty ();

jquery Operation Select

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.