Four types of jquery dynamic add option drop-down box code

Source: Internet
Author: User

Four types of jquery dynamic addition of option drop-down box code this article adds four examples of using jquery dynamic addition of option drop-down box code, with this jquery option instance, you can easily operate the option item. If you want to delete the option item, you can delete it.

Four types of jquery dynamic add option drop-down box code
This article has added four instance codes that use jquery to dynamically add option drop-down boxes. With this jquery option, you can easily operate option items and delete and modify them.

Function loadsteel_num (typeid ){
Var drp2 = $ ("drsteel_num ");
Drp2.options. length = 0;
Downloadurl ('bsorder _ steel_num.asp tutorial x? B _typeid = '+ typeid, function (xml, responsecode ){
Var dom
If (document. all) // ie
{
Dom = new activexobject ("microsoft. xmldom ");
Dom. loadxml (xml)
}
Else if (window. domparser) // firefox
Dom = new domparser (). parsefromstring (xml, "text/xml ");
If (dom)
{
Items1 = dom. getelementsbytagname ("item ");
Var itemslength = items1.length;
For (I = 0; I <itemslength; I ++)
{
Var newoption = document. createelement ("option ");
Newoption. text = items1 [I]. getattribute ("name ");
Newoption. value = items1 [I]. getattribute ("name ");
Drp2.options. add (newoption );
}
}
});

 


<Items>
<Item name = "abc"> </item>
</Items>

// Method 2

$ ("# Selectid"). append ("<option value = '" + value + "'>" + text + "</option> ");

Of course, in addition to this sentence, there are also set the default value, the first value, the last value, the n value, and so on, so I searched the internet:

Jquery gets the selected text and value of select:

1. $ ("# select_id"). change (function () {// code...}); // Add an event for select, triggered when one of the select events is selected

2. var checktext = $ ("# select_id"). find ("option: selected"). text (); // obtain the selected text

3. var checkvalue = $ ("# select_id"). val (); // obtain the value selected by select

4. var checkindex = $ ("# select_id"). get (0). selectedindex; // obtain the index value selected by select

5. var maxindex = $ ("# select_id option: last"). attr ("index"); // obtain the largest index value of select.

 

Jquery adds/deletes select option items:

1. $ ("# select_id"). append ("<option value = 'value'> text </option>"); // append an option for select (drop-down)

2. $ ("# select_id "). prepend ("<option value = '0'> select </option>"); // insert an option for select (first position)

3. $ ("# select_id option: last"). remove (); // Delete the largest option (last) index value in select)

4. $ ("# select_id option [index = '0']"). remove (); // Delete the option with the index value 0 in select (the first option)

5. $ ("# select_id option [value = '3']"). remove (); // Delete the option with value = '3' In the select statement

5. $ ("# select_id option [text = '4']"). remove (); // Delete the option of '4' text = '4' in select

Jquery dynamically adds option Method 4

// First Verification
$ (Document). ready (function (){
$ ("# Listone"). dblclick (function (){
Alert ($ ("# listone option: selected"). text (); // The text and html here get the same result.
// Var temp = $ ("# listone option: selected"). text ();

// $ ("<Div>" mirror.html (temp). appendto ("... "); $ (" <Div> ") or $ (" <span> ") can be dynamically added
// Try $ ("<option>" cmd.html (temp). appendto ("# listtwo"); failed !!!
// Finally, change $ ("option" cmd.html (temp). appendto ("# listtwo"). If the file is loaded successfully, you still don't know why.
$ ("# Listone option: selected"). appendto ("# listtwo ");
});
});

<Div>
<Select size = "10" class = "mytext" id = "listone">
<Option> 1 </option>
<Option> 3 </option>
<Option> 4 </option>
<Option> 5 </option>
</Select>
<Div id = "title"> Add </div>
<Select size = "10" class = "mytext" id = "listtwo">
</Select>
<Div id = "auto"> </div>

</Div>
Css tutorial
<Pre class = java name = "code"> <style type = "text/css">
<! --
. Mytext {
Width: 200px;
Height: 200px;
Background-color: # ffffff;
Clear: right;
Float: left;
}
# Title {
Clear: right;
Float: left;
Padding-top: 80px;
Width: 70px;
Text-align: center;
}
-->
</Style>
</Pre>
<Br>

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.