表单元格遍历jQuery



我看过很多相关的帖子,但在这里什么都不管用。因此,我试图验证每次按键时输入的数据,并在下一个div(在下一单元格中)中显示错误,尝试了许多遍历组合,但没有成功。

代码如下:-

$(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>

假设$(this)实际上正在选择input元素,您还需要调整if语句来检查输入框长度的,而不是选择器的长度:

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

此外,我假设您在当前脚本代码周围有这个选择器:

$("input[type='text']").each(function(){
   $(this).keypress( function() {
   ...
   });
});

这是你的代码和我的输入。。。工作

最新更新