Today, I encountered a problem about broken eggs:
1. I click an event from a drop-down list to obtain the value of options.
Copy codeThe Code is as follows:
Var product_id = $ (this). val ()
Console. log is an array, for example, ["51"]
Then I made the following judgment:
Copy codeThe 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:
Copy codeThe Code is as follows:
Var result = $. inArray (product_id, arr_product_ids );
Correct code:
Copy codeThe 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.