Jquery obtains the select value code

Source: Internet
Author: User

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

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.