尝试捕获布尔假值的另一个条件 [英] Trying to catch another condition for boolean false value

查看:69
本文介绍了尝试捕获布尔假值的另一个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

游戏演示

我的标志的结果始终为true,我不知道为什么.如果没有重复,我需要获取标志值'true'.请参见上面的小提琴:

My flag always has a result of true , and I dont know why. I need to get the flag value 'true', if nothing is duplicate.Please see fiddle above:

  if (readOnlyInput.length) {
            flag = true;
            inputs.not(readOnlyInput).closest('tr').css('background-color', 'red');
        } else {
             flag = true;
            inputs.not(':first').closest('tr').css('background-color', 'red');
        }

对于那些投票否决的人,我确实将标志设置为true,因为条件是true,我现在的问题是要添加条件以便我可以将其设置为false.将那些标志设置为true是正确的.现在,我应该添加条件,以便可以将其设置为false,但是我不知道要添加什么条件.

To does who downvoted, I do set the flag to true to true because the condition is true, My problem now is what to add condition so i can set it false.Setting those flag to true is correct. Now, I should have add condition so that I can set it to false, but i dont know what condition I will add.

推荐答案

尝试在迭代开始时将flag初始化为true. 并且如果迭代中的当前条目违反了规则(重复项),则将flag设置为false.

Try to initialize your flag to true in the beginning of the iteration. And if the current entry in the iteration violates the rule (a duplicate), set the flag to false.

已更新:

  $(document).on("click", '.tdAdd', function () {
    var counter = $('#myTable tbody tr').length + 1;
    var newRow = $("<tr>");
    var cols = "";
    cols += '<td><input type="button" value="Add Row" class="tdAdd"/></td>';
    cols += '<td><input type="button" value="Delete" class="tdAdd"/></td>';
    cols += '<td><input type="text" name="name' + counter + '"/></td>';
    cols += '<td><input type="text" name="price' + counter + '"/></td>';
    newRow.append(cols);
    newRow.insertAfter($(this).closest("tr"));
});

$("#save").off("click").on("click", function () {
    // Clear status of all elements
    $('#myTable tr').css('background-color', 'none');

    // Get all values first (Apple, Orange, etc) as strings
    var allValues = $('#myTable td').filter(function () {
        return $(this).closest('tr').children('td').index(this) === 2;
    })
        .find('input').map(function () {
        return $(this).val();
    }).toArray();
    // Iterate unique values individually, to avoid marking a read-only input as duplicate
    var flag = true;
    for (var i = 0; i < allValues.length; i++) {
        var value = allValues[i];
        if (value == '') continue; //blank field
        var inputs = $('#myTable td').filter(function () {
            return $(this).closest('tr').children('td').index(this) === 2;
        })
            .find('input').filter(function () {
            return $(this).val() == value;
        });
        // Check if this value is in one of the readonly
        var readOnlyInput = inputs.filter(function () {
            return $(this).is('[readonly]');
        });
        if (inputs.length > 1) {
            flag = false;
            if (readOnlyInput.length >= 1) {
                inputs.not(readOnlyInput).closest('tr').css('background-color', 'red');
            } else {
                inputs.not(':first').closest('tr').css('background-color', 'red');
            }
        }
        //console.log('Finished processing of ' + value);
    }
    alert(flag);
});

这篇关于尝试捕获布尔假值的另一个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆