我正在創建自己的標記「thingy」,它的功能與Stack Overflow的功能類似。檢查對陣列的值
但是,我的標籤要求我檢查兩個類別(數組),AmericanCodes
和CanadianCodes
。如果標籤不存在於兩個數組中的任何一箇中,我需要以不同的方式處理它。
似乎一切正常,除了我的$.inArray
功能。
它的行爲就好像它每次都在第一個數組中找到一樣。 (你會明白我的意思是,當你查看小提琴)
這裏是我的代碼:小提琴以下鏈接
$(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("");
}
});
});