2014-02-05 28 views
0

我有一些文本域,我想检查它们是否有文本。jquery - 检查文本域中是否有文本

<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"); 
} 

回答

0

目前,你检查您的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"); 
    } 
}); 
+0

这样做的窍门!非常感谢。 – RedHawkDK

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"); 
} 
+0

它现在没有做任何事情。它停止运行我所有的其他代码。你认为这可能是什么? – RedHawkDK

+0

如果您需要更多代码或信息,请告诉我:D – RedHawkDK

+0

@RedHawkDK您可以创建一个jsfiddle示例http://jsfiddle.net/ –