Search array inArray but ignore case

浪尽此生 提交于 2019-12-08 02:45:39

问题


Hope I provided a clear title.

I am creating an array comprised of all options, trimming whitespace, and storing. (correct me if im wrong in my process). On input change, if option array returns false, clear the next field, else, store the unique new value and place in next field.

If array contains:

blue
black
brown

and I search for BLUE or Blue or BLue...etc, the result returns false. I guess im asking for caseInsensitive logic, but not sure how I can incorporate.

Ive done this all on trial and error so please feel free to refine if your time allots. Also, this needs to be within spec of jquery 1.3

http://jsfiddle.net/arkjoseph/dRpmq/2/

thank you for assistance.


回答1:


Since it looks like you have control of the values (and are making them lowercase), you can do a pretty simple change from:

if (($.inArray((this.value), option) > 0)

to

if (($.inArray((this.value.toLowerCase()), option) > 0)

Demo at http://jsfiddle.net/dRpmq/3/




回答2:


From Case insensitive search in array JavaScript provide a Method indexOf() which is used to search object from array but this is case sensitive.

Jquery provide a method jQuery.inArray() To search object in array but this is case sensitive also i.e “a” is not equal to “A”. So we have to make our own function, Following is small javascript function which return index of object if found in array otherwise return -1 and this is case insensitive method



来源:https://stackoverflow.com/questions/15102429/search-array-inarray-but-ignore-case

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!