简体   繁体   English

jQuery.validate errorPosition无法正常工作

[英]jQuery.validate errorPosition not working correctly

jQuery.validate errorPosition code : jQuery.validate errorPosition代码

errorPlacement: 
     function(error, element)
     {
          error.appendTo(element.siblings("label"));  
     },

That code works brilliantly when there is only one element below it's label. 当标签下面只有一个元素时,该代码将非常有效。

Does NOT work: 不起作用:

<label for="location">Location:</label><br />
<div id="location"></div><br />
<input name="location" id="h_location" hidden /><br />

This works: 这有效:

<label for="location">Location:</label><br />
<input name="location" id="h_location" hidden /><br />

When the div location is not there the error shows next to the location label. 当div位置不存在时,错误会显示在位置标签旁边。 When the location div is there the error message does not show at all. 当位置div出现时,错误消息根本不显示。

I've been asking around for help and someone mentioned to me that I should read up on CSS block level elements. 我一直在寻求帮助,有人向我提到我应该阅读CSS块级元素。 I read up on them and I'm not finding a solution. 我仔细阅读了它们,但没有找到解决方案。 All I gathered from that reading is, how to display either: inline, block or none. 我从阅读中收集的全部内容是如何显示:内联,阻止或不显示。 I'm apparently not understanding what that person is hinting towards. 我显然不明白那个人在暗示什么。

Why is the error message not showing when it should and how do I get it to show? 为什么错误消息什么时候不显示?如何显示?

-Thank You, Rich -谢谢你,有钱人

I found a way to attach the error message to the label: 我找到了一种将错误消息附加到标签的方法:

var elementName = element.attr("name");

error.appendTo($("label[for=" + elementName + "]"));

That works as expected. 那按预期工作。

I'm still open for other ways to accomplish this. 我仍然对实现此目标的其他方式持开放态度。

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

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