JS jQuery - check if value is in array

后端 未结 4 1288
野的像风
野的像风 2021-02-05 07:05

I am more of a PHP person, not JS - and I think my problem is more a syntax problem ..

I have a small jQuery to \"validate\" and check input value .

It works ok

4条回答
  •  借酒劲吻你
    2021-02-05 07:38

    You are comparing a jQuery object (jQuery('input:first')) to strings (the elements of the array).
    Change the code in order to compare the input's value (wich is a string) to the array elements:

    if (jQuery.inArray(jQuery("input:first").val(), ar) != -1)
    

    The inArray method returns -1 if the element wasn't found in the array, so as your bonus answer to how to determine if an element is not in an array, use this :

    if(jQuery.inArray(el,arr) == -1){
        // the element is not in the array
    };
    

提交回复
热议问题