JS, jquery, dynamic add Option Item

Source: Internet
Author: User

    1. var selid = document.getelementbyid (" Sltid ");   
    2.   
    3.        for (var i= 0; i<10;i++) {      //Loop adds multiple values   
    4.   
    5.                sid.option[i] = new  option (i,i);   
    6.   
    7.         }  
    8.   
    9.         Sid.options[sid.options.length]=new option ("1", "2");   //  add one more after the last value   

Jquery:

[HTML]View Plaincopy
  1. <select id="AA"></Select>
  2. 1.jQuery ("<option value=' 1 '>abc</option>"). AppendTo ("#aa");
  3. 2.jQuery ("<option></option>"). attr ({val: "1", Text: "ABC"}). AppendTo ("#  AA ");
  4. 3.jQuery ("<option></option>"). val ("1"). Text ("abc"). AppendTo ("#aa");
  5. 4.$ ("#selectId"). Append ("<option value='" +value+ "'>" +text+ "</option  > ");


Of course, in addition to these words, but also set the default selection value, the first value, the last value, the nth value and so on, so on the internet searched:

jquery Gets the text and value selected by select:

[HTML]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. $ ("#select_id"). Change (function () {//code ...}); Add an event for Select, which is triggered when one of the items is selected
    2. var checktext=$ ("#select_id"). Find ("option:selected"). Text (); Gets the text selected by the Select
    3. var checkvalue=$ ("#select_id"). Val (); Gets the value selected by Select
    4. var checkindex=$ ("#select_id"). Get (0). SelectedIndex; Gets the index value of the select selection
    5. var maxindex=$ ("#select_id option:last"). attr ("index"); Gets the maximum index value of the Select


jquery Add/Remove option for select:

[HTML]View Plaincopy
    1. 1. $ ("#select_id"). Append ("<option value=' value '>text</option>"); Append an option to the Select ( drop-down item)
    2. 2. $ ("#select_id"). Prepend ("<option value=' 0 '> Please select </option>"); Insert an option for select (first position)
    3. 3. $ ("#select_id option:last"). Remove (); Delete index value in select maximum option (last)
    4. 4. $ ("#select_id option[index=' 0 ')". Remove (); Delete option with index value 0 in select (first)
    5. 5. $ ("#select_id option[value=' 3 ')". Remove (); Delete option for value= ' 3 ' in select
    6. 5. $ ("#select_id option[text=' 4 ')". Remove (); Delete option for text= ' 4 ' in select

JS, jquery, dynamic add Option Item

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.