indexof 在变量为空或指定值之前不会更改,即使它应该检查每个 KeyUp 事件也是如此

indexof doesn't change until the variable is empty or a specified value even though it should check on every keyup event

本文关键字:检查 KeyUp 事件 变量 indexof      更新时间:2023-09-26

>我无法在每次键上更新我的兼容性索引布尔值,因为它所依赖的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;
    }
}

我在你的代码中看到了很多其他冗余的例子。再次,我很抱歉 - 我知道你没有征求这个建议,但我相信从长远来看,它会帮助你找到你遇到的任何问题。