SoFunction
Updated on 2025-03-09

Get the value of the drop-down list box is array, split, $.inArray example

Today I encountered a broken problem:

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

Copy the codeThe code is as follows:

var product_id = $(this).val()

It comes out and finds that it is an array, such as: ["51"]

Then the following judgment was made
Copy the codeThe code is as follows:

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

You can pop up the prompt box if you find it

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

Error code:
Copy the codeThe code is as follows:

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

Correct code:
Copy the codeThe 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.

Only today do I know that the value obtained by the drop-down list is an array. I have a deeper understanding of it. Please explain it.