Gets the value of the Drop-down list box is an array, split,$.inarray example _javascript tips

Source: Internet
Author: User
Today I met a thief with a broken egg:

1. I click events from a drop-down list select to get the value of the options
Copy Code code as follows:

var product_id = $ (this). Val ()

Console.log out is found to be a number of groups, such as: ["51"]

And then make the following judgments
Copy Code code as follows:

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

Found can be pop-up balloon, ni Ma This is not a hole in me?

2, I use this product_id to match whether or not to be included in an array

Error code:
Copy Code code as follows:

var result = $.inarray (product_id,arr_product_ids);

Correct code:
Copy Code code as follows:

var result = $.inarray (product_id[0],arr_product_ids);

$.inarray () must use product_id[0], that is, cannot use an array

It is not until today that the Drop-down list gets the value of the number group, with a deeper understanding of the great God, to explain.

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.