简体   繁体   English

jQuery忽略null检查if语句

[英]Jquery ignoring null check if statement

I have a function that adds table rows on button click which appends an number counter per table row addition to the rows id. 我有一个在单击按钮时添加表行的功能,该功能在每个表行添加到行ID的后面添加一个数字计数器。 I have a button click that iterates the # of times based on that counter and then it grabs the id's of an with the number counter and checks if they have a value. 我有一个按钮单击,它基于该计数器重复#次,然后它使用数字计数器获取ID的ID,并检查它们是否具有值。

CODE: 码:

$('#showCounter').click(function () {
            for (var i = 1; i < idCounter; i++) {
                if ($('[id$="txtName' + i + '"]').val() !== "" || $('[id$="txtName' + i + '"]').val() !== null) {
                    console.log($('[id$="txtName' + i + '"]').val());
                }
            }
        });

So theoritically, it should only console.log when the textbox' has value but right now so for example I have 3 table rows and 1 row has value, it will console.log all 3 rows. 因此,从理论上讲,当文本框具有值时,它仅应console.log,但是现在,例如,我有3个表行,而1行具有值,它将console.log所有3行。 The if statement condition seems to have never been met. if语句条件似乎从未得到满足。

Any help would be appreciated thanks. 任何帮助,将不胜感激谢谢。

just do: 做就是了:

if ($('[id$="txtName' + i + '"]').val()) {
  console.log($('[id$="txtName' + i + '"]').val())
}

The value of a field can not be null, it's always a string value .So do like below:- 字段的值不能为null,它始终是字符串值。因此请执行以下操作:-

$('#showCounter').click(function () {
    for (var i = 1; i < idCounter; i++) {
        if ($('[id$="txtName' + i + '"]').val() !== "") {
            console.log($('[id$="txtName' + i + '"]').val());
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM