Get the value of the drop-down list box as an array, split, $. inArray example _ javascript

Source: Internet
Author: User
Get the value of the drop-down list box is an array, and then use product_id to match whether it is contained in an array. If you are interested, do not miss the problem of a zombie today:

1. I click an event from a drop-down list to obtain the value of options.

The Code is as follows:


Var product_id = $ (this). val ()


Console. log is an array, for example, ["51"]

Then I made the following judgment:

The Code is as follows:


Console. log (product_id );
If (product_id = '51') {alert (111 );}
If (product_id [0] = '51') {alert (222 );}


The prompt box is displayed. Isn't Nima a pitfall for me?

2. I use this product_id to match whether it is contained in an array.

Error code:

The Code is as follows:


Var result = $. inArray (product_id, arr_product_ids );


Correct code:

The Code is as follows:


Var result = $. inArray (product_id [0], arr_product_ids );


$. InArray () must use product_id [0], that is, arrays are not allowed.

The value obtained from the drop-down list is an array. Please explain it.

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.