【问题标题】:jquery - Check if there is text in a textfieldjquery - 检查文本字段中是否有文本
【发布时间】:2014-02-05 09:24:29
【问题描述】:

我有一些文本字段,我想检查其中是否有文本。

<input type="text" class="inputField" id="a113" name="grp113">cm</div>

您能帮我找出以下代码中的错误吗? :)

if ($(this).find('input[type="text"]').length > 0) 
{
    alert("there is text in the field");
}
else
{
    alert("there is no text in the field");
}

【问题讨论】:

    标签: jquery search input find


    【解决方案1】:

    .find()返回一个jQuery对象,需要获取文本字段的值——可以使用.val()获取输入字段的值

    if ($(this).find('input[type="text"]').val().length > 0) {
        alert("there is text in the field");
    } else {
        alert("there is no text in the field");
    }
    

    【讨论】:

    • 它现在什么都不做。它阻止我所有其他代码运行。你认为它可能是什么?
    • 感谢您回答我的问题。
    【解决方案2】:

    目前,您正在检查 DOM 中是否有任何文本框。我怀疑您想要以下内容:

    $(this).find('input[type="text"]').each(function () {
        if ($(this).val().length > 0) {
            alert("there is text in the field");
        } else {
            alert("there is no text in the field");
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-05
      • 2016-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 2021-07-01
      • 2015-12-28
      相关资源
      最近更新 更多