JQuery implements the Select drop-down list to Select the status, jqueryselect

Source: Internet
Author: User

JQuery implements the Select drop-down list to Select the status, jqueryselect

Scenario:

E. g.: When managing a blog post, the management of a blog post includes a column named "status", which has many statuses, such:Normal, waiting for review, deletion, etc...In this case, if you use the Select drop-down list to Select the status and Select a specific item value, and submit it asynchronously through Ajax, you can enjoy a larger experience in terms of effect and experience.

An example of the drop-down list is as follows:

<Select id = "status"> <option value = "0"> to be reviewed </option> <option value = "1"> not approved </option> <option value = "2"> production in progress </option> <option value = "3"> production completed </option> <option value = "4"> release </option> <option value = "5"> suspend </option> <option value = "6"> Delete </option> </select>

Error example:

 $("select#status").click(function(){ console.log($(this).val()); });

If you run the click event, an Ajax request is triggered for the first time in the drop-down list. This does not conform to the logic. Therefore, you cannot use the click event as the method to select a specific value in the drop-down list.

Correct example:

 $("select#status").change(function(){ console.log($(this).val()); });

W3SCHOOL's interpretation of the change event is as follows:

Definition and usage

When the value of an element changes, a change event occurs.

This event applies only to text fields and textarea and select elements.

The change () function triggers the change event, or specifies the function that runs when a change event occurs.

Note: When used for select elements, the change event occurs when an option is selected. When used in text field or text area, this event occurs when the element loses focus.

The above shows that the Change event should be used for the Select drop-down list.

Supplement:

Because Ajax technology is also used for asynchronous page turning, the original method will fail after page turning, and the following code should be used for execution:

 $(document).on("change",'select#status',function(){ console.log($(this).val()); });

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

Related Article

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.