2014-02-05 30 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/ –