The value to get the drop down list box is the array split $.inarray example

  • 2020-03-29 23:44:57
  • OfStack

Today we had a problem with broken eggs:

1. I click on the event from a drop-down select list to get the value of the options
var product_id = $(this).val() 

Console. Log is found to be an array, such as: ["51"]

Then he made the following judgment
if(product_id == '51'){alert(111);} 
if(product_id[0] == '51'){alert(222);} 

Find can pop up prompt box, isn't this a pit me

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

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

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

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

Only today did I realize that the value of the drop-down list is an array.

Related articles: