2012-02-09 153 views
1

我見過很多相關的帖子,但這裏沒有任何東西在爲我工作。 所以在這裏,我試圖驗證每個按鍵上的輸入數據,並在下一個div(在下一個單元格)中顯示錯誤,嘗試了許多遍歷組合,但沒有運氣。下面表格單元遍歷jQuery

代碼給出: -

$(this).keypress(function() { 
//alert(this); 
if (($(this).val().length < 5)||($(this).val().length > 20)) // maybe not working 
$(this).parent('td').next('td').children('div').text("Error"); // <- not working 
// check input for validity here 
}); 

表: -

<table width="1032" height="448" align="center" cellpadding="1" cellspacing="0"> 
    <tr> 
    <th width="153" align="right" scope="row"><strong>Name:</strong></th> 
    <td width="420"> 
     <label for="user_name"></label> 
    <input name="user_name" type="text" id="user_name" size="70" maxlength="100" value="<?php if (isset($row)) echo $row[1]; ?>"/> 

    </td> 
    <td width="451"> 
    <div id="div_user_name"></div> 
    </td> 
    </tr> 
</table> 
+0

修正if語句 – SAQIBZAFAR 2012-02-09 07:37:40

+0

我們有走!檢查我最新的編輯與jsfiddle的例子,工作好... :) – 2012-02-09 07:39:44

回答

1

假設$(this)實際上是在選擇你的input元素,你還需要調整if語句來檢查輸入框長度的,而不是選擇器的長度:

if (($(this).val().length < 5) || ($(this).val().length > 20)) 

另外,我假設您對您目前的腳本代碼這個選擇:

$("input[type='text']").each(function(){ 

    $(this).keypress(function() { 
    ... 
    }); 

}); 

我輸入代碼的Here's a jsfiddle ...的工作...

+0

是下一行好嗎?因爲當我評論它沒有工作 – SAQIBZAFAR 2012-02-09 07:31:53

+0

檢查,並糾正,不工作仍然。 – SAQIBZAFAR 2012-02-09 07:34:12

+0

非常感謝,沒有選擇前,按鍵功能:( – SAQIBZAFAR 2012-02-09 07:39:04