简体   繁体   English

jQuery Validator中的errorPlacement问题

[英]Problem With errorPlacement in jQuery Validator

I have a question about the errorPlacement Method in jQuery Validator Plug-in: I want to put an Error Message in a div. 我对jQuery Validator插件中的errorPlacement方法有疑问:我想在div中放置一条错误消息。

Before i continue... Yeah, the code is horrible, i'm working in a project that isn't mine, without documentation or anything that i help me. 在我继续之前...是的,代码太可怕了,我在一个非我的项目中工作,没有文档或任何我需要帮助的东西。 But i cannot write from zero all the webpage. 但是我无法从零开始编写所有网页。

Ok, let's continue, this is a little section of the HTML code: 好的,让我们继续,这是HTML代码的一小部分:

<tr>
  <th height="24" scope="col">
    <div align="left" class="Style69 Style72 Style75">
      <span class="Style77">Name</span>
    </div>
  </th>
  <th height="24" colspan="3" scope="col">
    <div align="left">
      <span class="Style69 Style72 Style75">
        <input id="txtname" name="txtname" class="required"
        type="text" size="30" />
      </span>
    </div>
  </th>
</tr>
<tr>
  <td height="17" colspan="4">
    <div id="errorMessage"></div>
  </td>
</tr>

The Layout is a table, and, inside of this table are placed all the elements (inputboxes and select's). 布局是一个表,在该表的内部放置了所有元素(输入框和选择框)。

My question is, how can i place the error message in the div "errorMessage"? 我的问题是,如何将错误消息放入div“ errorMessage”中?

I'm doing something like this: 我正在做这样的事情:

errorPlacement: function(error, element) {
   error.appendTo(element.prev().next().next());
 }

But it doesn't work. 但它不起作用。 Then, I've Tried This: 然后,我尝试了以下方法:

errorPlacement: function(error, element) {
   error.appendTo(element.prev().next().next().find("#errorMessage"));
 }

And nothing happens. 没有任何反应。 The "Div" and "Span" tags are part of the DOM? “Div”和“Span”标签是DOM的一部分?

Any Suggestion? 有什么建议吗?

The problem here is that .prev() gives no results, there's no previous sibling to your <input> here: 这里的问题是.prev()没有给出结果,这里的<input>没有以前的同级项:

  <span class="Style69 Style72 Style75">
    <input id="txtname" name="txtname" class="required"
    type="text" size="30" />
  </span>

Something like this should work: 这样的事情应该有效:

errorPlacement: function(error, element) {
  element.closest('tr').next().find('.errorMessage').append(error);
}

This uses .closest() to go up to the <tr> , gets the .next() sibling (the next <tr> ) then .find() to get the class="errorMessage" element inside. 它使用.closest()上至<tr> ,获取.next()同级(下一个<tr> ),然后.find()以获取内部的class="errorMessage"元素。

With this, change your id to a class, like this: 这样,将您的id更改为类,如下所示:

<div class="errorMessage"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM