indexof 在变量为空或指定值之前不会更改,即使它应该检查每个 KeyUp 事件也是如此
indexof doesn't change until the variable is empty or a specified value even though it should check on every keyup event
>我无法在每次键上更新我的兼容性索引布尔值,因为它所依赖的indexof的值仅在其中一个搜索值被清空或具有指定值(如"米饭"或"红酒")时自行更新。有谁知道,为什么即使它应该在每个 keyup 事件上检查它,也会发生这种情况?
<div class="row">
<h1 class="big col-xs-12">Prepare your dinner according to the reference of mixing rules to attain the optimum harmony of taste.</h1>
</div>
<hr />
<h2 class="searchQ">Main ingredient</h2>
<input type="text" name="ingredient1" class="search search1" value="">
<hr />
<h2 class="searchQ">Complementary ingredient</h2>
<input type="text" name="ingredient2" class="search search2" value="">
<hr />
<h1>You <span class="can">can</span> mix <span class="searchVal1"></span> <span class="and"></span> <span class="searchVal2"></span> with:</h1>
<ul class="list1"></ul>
<ul class="list2"></ul>
<script>
var redWine = ['Red Wine','Cheese','Cured Meats','Red Meat'];
var rice = ['Rice','White Wine','Chicken'];
var whiteWine = ['White Wine','Chicken','Oyster','Rice'];
var notFound = "Nope, don't think you can eat that";
var search = $('.search');
var search1 = $('.search1');
var search2 = $('.search2');
var search1conv = [];
var search2conv = [];
var compatibilityIndex = false;
var can = $('.can');
var searchVal1 = $('.searchVal1');
var and = $('.and');
var searchVal2 = $('.searchVal2');
var list1 = $('.list1');
var list2 = $('.list2');
function checking(checkedVal, searchConv, list) {
switch ( checkedVal.toLowerCase() ) {
case 'red wine':
case 'wine red':
case 'wine, red':
list.empty();
searchConv = redWine;
for (var i = 1; i < redWine.length; i++) {
list.append('<li>' + redWine[i] + '</li>');
}
break;
case 'rice':
list.empty();
searchConv = rice;
for (var i = 1; i < rice.length; i++) {
list.append('<li>' + rice[i] + '</li>');
}
break;
case 'white wine':
case 'wine white':
case 'wine, white':
list.empty();
searchConv = whiteWine;
for (var i = 1; i < whiteWine.length; i++) {
list.append('<li>' + whiteWine[i] + '</li>');
}
break;
default:
list.empty().append('<h2>' + notFound + '</h2>');
break;
}
}
var compatible = function(ing1, ing2) {
if ( ing1.indexOf(ing2[0]) > -1 && ing2.indexOf(ing1[0]) > -1 ) {
compatibilityIndex = true;
} else {
compatibilityIndex = false;
}
}
var main = function() {
search.keyup(function() {
if (search1.val() != 0 || search2.val() != 0) {
if (search1.val() != 0) {
checking( search1.val() , search1conv , list1 );
} else {
list1.empty();
}
if (search2.val() != 0) {
checking( search2.val() , search2conv , list2 );
} else {
list2.empty();
}
} else {
searchVal1.empty();
searchVal2.empty();
list1.empty();
list2.empty();
}
if (search1.val() != 0 && search2.val() != 0) {
compatible(search1conv, search2conv);
if (compatibilityIndex) {
can.text('can').removeClass('red').addClass('green');
} else {
can.text('can''t').removeClass('green').addClass('red');
}
searchVal1.text( search1.val() );
and.text('and' + " " + search1conv.indexOf(search2conv[0]) + " " + search2conv.indexOf(search1conv[0]));
searchVal2.text( search2.val() );
} else {
if (search1.val() == 0) {
can.text('can').removeClass('red').removeClass('green');
searchVal1.empty();
and.empty();
} else {
searchVal1.text( search1.val() );
}
if (search2.val() == 0) {
can.text('can').removeClass('red').removeClass('green');
searchVal2.empty();
and.empty();
} else {
searchVal2.text( search2.val() );
}
}
});
}
$(document).ready(main);
</script>
首先,让我道歉,因为这不是你问题的答案,但是当我开始浏览你的代码时,我看到变量和函数中存在大量冗余。这使代码更复杂且更难调试。我将从简化代码开始。例如,您不需要 checking1 和 checking2 函数 - 它们都做同样的事情,它们只是对不同的数据进行操作。通过将更多数据传递给函数,您可以为两组数据使用一个函数:
function checking(checkedVal, searchConv, list) {
switch ( checkedVal.toLowerCase() ) {
case 'red wine':
case 'wine red':
case 'wine, red':
list.empty();
searchConv = redWine;
for (var i = 1; i < redWine.length; i++) {
list.append('<li>' + redWine[i] + '</li>');
}
break;
case 'rice':
list.empty();
searchConv = rice;
for (var i = 1; i < rice.length; i++) {
list.append('<li>' + rice[i] + '</li>');
}
break;
case 'white wine':
case 'wine white':
case 'wine, white':
list.empty();
searchConv = whiteWine;
for (var i = 1; i < whiteWine.length; i++) {
list.append('<li>' + whiteWine[i] + '</li>');
}
break;
default:
list.empty().append('<h2>' + notFound + '</h2>');
break;
}
}
我在你的代码中看到了很多其他冗余的例子。再次,我很抱歉 - 我知道你没有征求这个建议,但我相信从长远来看,它会帮助你找到你遇到的任何问题。
相关文章:
- 为什么不't Javascript对我的输入值进行了一些重新检查
- keyup事件处理程序更改焦点不适用于快速键入
- JQuery合并了keyup和focusout两个函数
- 在单击任何位置时隐藏元素,而不检查每次DOM单击
- 如何检查管道中未定义的项目
- 主干-不管怎样,检查事件以前是否绑定过
- 使用javascript检查多个输入值,并在1次检查中标记多个输入框
- 递归使用 eval() 是检查程序执行的好方法吗?
- 检查onsubmit doenst work jquery contactform上的函数
- JavaScript 检查隐藏字段中的值并将其设置为隐藏字段值
- API密钥使用和检查示例
- 如何让程序检查所选单词中是否有按键
- JS:检查URL中的参数,然后迭代一个参数为var的函数
- 需要在keyup函数退出时检查长度
- j查询“keyup”在检查“字数统计”时崩溃页面
- 角度 js ng-keyup 不适用于密码强度检查
- indexof 在变量为空或指定值之前不会更改,即使它应该检查每个 KeyUp 事件也是如此
- 检查事件是否已绑定(KEYUP)
- 检查Javascript中的keyup事件
- jQuery -检查输入内容是否包含url在keyup上