Summary of Select option operation in Jquery/js

Source: Internet
Author: User

1. Get the value and text,html code for select Select as follows:

The code is as follows Copy Code

<select id= "Myselect" >
<option value= "1" >one</option>
<option value= "2" >two</option>
<option value= "3" >three</option>
</select>

jquery Select operation

The code is as follows Copy Code


$ ("#mySelect"). Val (); Gets the value of the selected record
$ ("#mySelect option:selected"). Text (); Gets the text value of the selected record


jquery Gets the text and value selected by select:

The code is as follows Copy Code

1. Var checktext=jquery ("#select_id"). Find ("option:selected"). Text (); Gets the text selected by select

2. Var checkvalue=jquery ("#select_id"). Val (); Get option Value for select

3. Var checkindex=jquery ("#select_id"). Get (0). SelectedIndex; Get the index value selected by select

4. Var maxindex=jquery ("#select_id option:last"). attr ("index"); Get the maximum index value for select

jquery Add/Remove option items for select:

The code is as follows Copy Code

1. JQuery ("#select_id"). Append ("<option value= ' value ' >Text</option>"); Append an option to select (dropdown)

2. JQuery ("#select_id"). Prepend ("<option value= ' 0 ' > Please select </option>"); Inserts an option for the Select (first position)
3. JQuery ("#select_id option:last"). Remove (); Delete index value maximum option (last) in select

4. JQuery ("#select_id option[index= ' 0 ']"). Remove (); Delete option with index value of 0 in select (first)

5. JQuery ("#select_id option[value= ' 3 ']"). Remove (); Delete option value= ' 3 ' in select

6. JQuery ("#select_id option[text= ' 4 ']"). Remove (); Delete option text= ' 4 ' in select

Empty content:

The code is as follows Copy Code

JQuery ("#select_id"). empty ();

JavaScript Select operation

2, the use of new option ("Text", "value") method to add Options option

The code is as follows Copy Code

var obj = document.getElementById ("Myselect");
Obj.add (New Option ("4", "4"));

3, delete all options option

The code is as follows Copy Code

var obj = document.getElementById ("Myselect");
obj.options.length = 0;

4, Delete selected options option

The code is as follows Copy Code

var obj = document.getElementById ("Myselect");
var index = Obj.selectedindex;
Obj.options.remove (index);

5, modify the selected options option

The code is as follows Copy Code

var obj = document.getElementById ("Myselect");
var index = Obj.selectedindex;
Obj.options[index] = new Option ("three", 3); Change the corresponding value
Obj.options[index].selected = true; Remain selected

6. Delete Select

The code is as follows Copy Code

var obj = document.getElementById ("Myselect");
Obj.parentNode.removeChild (obj); Remove Current Object

7. Select Selected Response Event

The code is as follows Copy Code

$ ("#mySelect"). Change (function () {
Add the action code you need to perform
})

8. Option hidden and displayed in select

The code is as follows Copy Code
var sss = $ ("select option"). Map (function () {
return $ (this). Val ();
});

Use Sss[0] to get the value.

  code is as follows copy code
$ ("#select1 Option[value= ' 3 ']). Hide ();

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.