Home > Web Front-end > JS Tutorial > Get the value of the drop-down list box as an array, split, $.inArray example_javascript skills

Get the value of the drop-down list box as an array, split, $.inArray example_javascript skills

WBOY
Release: 2016-05-16 17:15:27
Original
1414 people have browsed it

I encountered a broken problem today:

1. I clicked the event from a drop-down list select to get the value of options

Copy Code The code is as follows:

var product_id = $(this).val()

When the console.log comes out, it is found to be an array. , such as: ["51"]

Then make the following judgment
Copy the code The code is as follows:

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

I found that a prompt box can pop up, isn’t this a trick on me?

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

Error code:
Copy code The code is as follows:

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

Correct code:
Copy code The code is as follows:

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

$.inArray() must use product_id[0], which means that arrays cannot be used

I didn’t know until today that the value obtained by the drop-down list is an array. If someone knows more about it, please explain.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template