In jquery, the select operation is much more convenient. If I want to obtain the selected select value, we can directly $ (\ '# s option: selected \'). val (). The following is a brief introduction to this topic.
Jquery obtains the selected text and value of select.
The Code is as follows: |
Copy code |
Obtain the selected text: jQuery ("# select1 option: selected"). text (); Obtain the value selected by select: $ ("# ddlregtype"). val (); Obtain the index selected by select: $ ("# ddlregtype"). get (0). selectedindex; |
The selected value is displayed.
The Code is as follows: |
Copy code |
<Script src = "jquery-1.9.1.js"> </script> <Script type = "text/javascript"> $ (Function (){ $ ('# Btn'). click (function (){ Alert ($ ('# s option: selected'). val ()); }) }) </Script> </Head> <Body> <Select id = "s" multiple = "multiple"> <Option value = "1"> operator </option> <Option value = "2"> Bing </option> <Option value = "3"> odd </option> </Select> <Input type = "button" id = "btn" value = ""/> <br/> </Body> |
Other select option operations
Set select option items:
The Code is as follows: |
Copy code |
$ ("# Select_id"). append ("<option value = 'value'> text </option>"); // Add an option $ ("# Select_id"). prepend ("<option value = '0'> select </option>"); // insert an option $ ("# Select_id option: last"). remove (); // Delete the option with the largest index value $ ("# Select_id option [index = '0']"). remove (); // delete an option with an index value of 0 $ ("# Select_id option [value = '3']"). remove (); // delete an option with a value of 3 $ ("# Select_id option [text = '4']"). remove (); // delete an option with a text value of 4 |