【问题标题】:Variable empty with if statement使用 if 语句为空的变量
【发布时间】:2017-10-13 22:07:36
【问题描述】:
$(".my-input").each(function () {
            var theSelectBoxContainer = $(this).parent().next();
            var theSelectBoxValue = theSelectBoxContainer.dxSelectBox('instance').option('value');

            var txtvalue = $(this).val();
            if (txtvalue != "" && txtvalue!=" ") {
                if (i)
                    field += " and ";
                field = field + "'" + $(this).val() + "'";                    
                i++;
            }
        });

在我的 jQuery 代码之上。使用此代码,我会覆盖在 TextBoxes 中输入的值。但是我不希望在输入空字符时写入文本框。但它是写的。我检查是否,但事实并非如此。哪里出错了?

【问题讨论】:

  • 简单使用 if(txtvalue.trim()) 验证 null,undefined and empty
  • if (txtValue.trim())
  • if(txtv​​alue.trim()) 正在工作。谢谢

标签: javascript jquery if-statement each


【解决方案1】:

您可以使用$.trim(txtvalue) 来验证空、空和未定义。

或者你也可以使用:

if (txtvalue === undefined || txtvalue === null) {
    // show error messages..
} else {
    //execute this code..
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 2019-04-17
    • 1970-01-01
    • 2014-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多