简体   繁体   English

根据数组检查值

[英]Checking Values Against of an Array

I'm creating my own tagging "thingy" which will function similar to Stack Overflow's. 我正在创建自己的标签“ thingy”,其功能类似于Stack Overflow的标签。

However, my tags require me to check against two categories(arrays), AmericanCodes and CanadianCodes . 但是,我的标签要求我检查两个类别(数组), AmericanCodesCanadianCodes If the tag does not exist in either of the two arrays, I need to deal with it differently. 如果标签在两个数组中都不存在,则需要以不同的方式处理它。

Everything seems to be working fine, with the exception of my $.inArray functionality. 除了$.inArray功能以外,其他所有功能似乎都正常运行。

It is acting as if it is found in the first array every time. 就像每次都在第一个数组中找到它一样。 (You'll see what i mean when you view the Fiddle) (当您查看小提琴时,您会明白我的意思)

Here is my code: Fiddle link below 这是我的代码:下面的小提琴链接

$(document).ready(function () {
    var AmericanCodes = ["AA", "AB", "AC", "AD", "AE", "AF"]; // Known american conversion codes
    var CanadianCodes = ["CA", "CB", "CC", "CD", "CE", "CF"]; // Known canadian conversion codes
    var UnknownCodes = []; // Collects Unrecognized Codes
    var Entry = $("input#RecessiveCodes"); // Set Input
    $(Entry).keypress(function (e) { // 
        if (e.keyCode == 32) { // This is the enter key
            var EntryValue = $(Entry).val(); // Get Input
            console.log(EntryValue);
            if ($.inArray(EntryValue, AmericanCodes) < 0) { // Check if it's an american code
                $("ul#EnteredCodes").append("<li class='AR'>", EntryValue, "</li>") // Style knowns slightly different than unkown
            } else if ($.inArray(EntryValue, CanadianCodes) < 0) { // Check if it's an canadian code
                $("ul#EnteredCodes").append("<li class='CR'>", EntryValue, "</li>") // Style knowns slightly different than unkown
            } else {
                UnknownCodes.push($(EntryValue)); //Add to list but store unknown reccessive seperatley
                $("ul#EnteredCodes").append("<li class='UR'>", EntryValue, "</li>") // Style unknowns slightly different
            }
            $(Entry).val("");
        }
    });
});

jsFiddle 的jsfiddle

jQuery.inArray(...) returns -1 if the value doesn't appear in the array. 如果值出现在数组中,则jQuery.inArray(...)返回-1

So: 所以:

if ($.inArray(EntryValue, AmericanCodes) < 0)

only matches if EntryValue does not exist in AmericanCodes . 仅在AmericanCodes 中不存在EntryValue匹配。

  • Try >= 0 instead. 请尝试>= 0

Couple of other things (maybe just from making the JSFiddle too quickly!): 其他几件事(可能只是使JSFiddle太快了!):

  • You've put the CSS as li.AR three times, instead of li.AR , li.CR and li.UR . 您已经将CSS作为li.AR了三遍,而不是li.ARli.CRli.UR

  • e.keyCode == 32 checks for the space key, not enter ! e.keyCode == 32检查空格键,不输入

  • If you are using space not enter , your $(Entry).val("") at the end of the function won't work because the keypress event is called BEFORE the most recently typed character was pressed, so the space key will be applied AFTER the text field is emptied. 如果您使用空格 not enter ,则函数末尾的$(Entry).val("")将不起作用,因为在keypress最近键入的字符之前调用了keypress事件,因此space键将为在文本字段清空后应用。 You probably want to use keyup , which is called after the keypress, but then remember to use trimRight() to remove the space character from the input before passing it to $.inArray . 您可能想使用keyup (在按键后调用),但是记住在传递给$.inArray之前,使用trimRight()input删除空格字符。

  • Event is already a jQuery object, so wrapping it as $(Event) is redundant and just wastes a few cycles. Event已经是一个jQuery对象,因此将其包装为$(Event)是多余的,并且仅浪费几个周期。

Working JSFiddle with all these changes: here 使用JSFiddle处理所有这些更改: 此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM