我正在尝试使用 javascript 来标记集合列表中的单词,但它似乎不起作用。基本上我希望用户能够输入一个单词,如果它与右侧列表中的任何一个匹配,它就会将该单词标记为红色。
知道我做错了什么吗?
http://jsfiddle.net/Copernicus76/rpuXW/embedded/result/
// Load the banned plates json data
var platesJson = (function () {
platesJson = null;
$.ajax({
'async': false,
'global': false,
'url': "bannedplates.json",
'dataType': "json",
'success': function (bannedplates) {
platesJson = bannedplates;
}
});
return platesJson;
})();
$(document).ready(function () {
// Draw the list //
$.each(platesJson, function (i) {
$('#plate-list').append('<div class="row-' + i + '">' + platesJson[i].plate + '</div>');
});
// Search the banned plates //
$('#plate-text').keyup(function () {
var result = '';
var plateRow = '';
var scrollPosition;
var searchText = $('#plate-text').val().toUpperCase();
$.each(platesJson, function (i) {
if (searchText == platesJson[i].plate) {
result = 'banned';
plateRow = i;
scrollPosition = i * 19;
}
});
if (/\S/.test(searchText)) {
if (result == 'banned') {
$('#plate-intro,#plate-ok').hide();
$('#plate-banned').show().fadeOut(1500);
$('#plate-text').addClass('banned');
$('#plate-list').animate({
scrollTop: scrollPosition
}, 1000);
$('#plate-list div.row-' + plateRow + '').addClass('banned');
} else {
$('#plate-intro,#plate-banned').hide();
$('#plate-ok').show().fadeOut(1500);
$('#plate-text').removeClass('banned');
}
} else {
$('#plate-text').val('');
$('#plate-ok,#plate-banned').hide();
$('#plate-intro').show();
}
});
}); // end document ready