JQuery implements the select drop-down box to obtain the selected text, value, index, and add/delete Select Option items, jqueryoption

Source: Internet
Author: User

JQuery implements the select drop-down box to obtain the selected text, value, index, and add/delete Select Option items, jqueryoption

// Directly Save the suffix. htnl and open it in Google's browser.
<Head> <script src =" http://libs.baidu.com/jquery/2.0.0/jquery.min.js "> </Script> <script> $ (function () {// Add an event for the Select statement. $ (" select: eq (0) ") is triggered when one of the Select statements is selected )"). change (function () {// code}); // obtain the selected Text of Select: the result is the text var checkText = $ ("select: eq (0): selected") composed of all the text content contained by the matching element "). text (); // we recommend that you use this simple =$ ("select: eq (0) option: selected "). tetx (); = $ ("# One "). find (": selected "). text (); = $ ("# One "). find ("option: selected "). text (); // if multiple values are selected, an array containing the selected value is returned. Var checkValue = $ ("# select_id "). val (); // obtain the current value of the selected Matching Element in the Select statement, that is, [only if multiple values are selected] obtain the val content of the First Matching Element var checkValue = $ ("select: eq (0): selected "). val (); // ========== this is strongly recommended, in case of multiple selections // obtain the selected index value var checkIndex = $ ("# select_id "). get (0 ). selectedIndex; // obtain the largest Select index value var maxIndex =$ ("# select_id: last "). prop ("index"); // we recommend that you use this =$ ("# select_id option: last "). prop ("index"); = $ ("select: eq (0 )"). find (": last "). prop ("inde X ") = $ (" select: eq (0 )"). find ("option: last "). prop ("index ") // ================================================ ========================================================== ==============/// jQuery sets the Text and Value selected by Select: // set the Select Value to 4. Select $ ("# select_id "). val (4); // use this $ ("# select_id [value = '4']"). prop ("selected", true); $ ("# select_id option [value = '4']"). prop ("selected", true); // sets the first option in select to be selected $ ("select: first "). prop ("se Lected ", true); // This $ (" select: first "). prop ("selected", 'selected'); $ ("select option: first "). prop ("selected", "true"); $ ("select option: first "). prop ("selected", "selected "); // ================================================ ========================================================== ====================/// jQuery adds/deletes the Select Option item $ ("# select_id "). append ("<option value = 'value'> Text </option>"); // append an Option to the end of the Select statement (drop-down) $ ("# select_ I D "). prepend ("<option value = '0'> Select </option>"); // insert an Option (first position) for the Select header $ ("# select_id: last "). remove (); // Delete the largest Option (last) of the index values in the Select statement $ ("# select_id: fist "). remove (); // The minimum index value in the delete Select statement is 0 Option (first) $ ("# select_id [value = '3']"). remove (); // Delete the Option with Value = '3' In the Select statement}); </script>

 

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.