自定义 Jquery 验证的行为

发布于 2024-12-22 06:19:29 字数 1960 浏览 1 评论 0原文

我正在使用 Jquery validate 向用户提供反馈,并向他们提供有关他们在表单中输入的详细信息的有效性的更新。但我在自定义 Jquery validate 创建的行为时遇到了麻烦。

我有一个像这样的简单表单:

<form id="form1">
<label for="input1" />
<input name="input1" />
<input type="submit" />
</form>

当用户输入无效信息时,我希望 Jquery 验证输出如下内容:

<form id="form1">
<label for="input1" />
<input name="input1" class="error"/><span class="errorIcon">Error</span>
<p class="errorText">Error message</p>
<input type="submit" />
</form>

当用户使用有效信息填写字段时,我希望 Jquery 验证输出:

<form id="form1">
<label for="input1" />
<input name="input1" /><span class="successIcon">Success</span>
<input type="submit" />
</form>

我已经设置了所需的规则和自定义验证消息,因此它们可以正常启动,但我无法获得上述行为。

我目前有这个:

$('#form1').validate({
 showErrors: function(errorMap, errorList) {
 if (errorList < 1 ) {
   $('span.errorIcon').remove();
   $('p.errorText').remove();
   $('input.error').removeClass('error');
   $('select.error').removeClass('error');
   return;
 }
 $.each(errorList, function(index, error) {
   if ($(error.element).siblings('.errorText').length === 0 && $(error.element).siblings('.errorIcon').length === 0)  {
     $(error.element).next('p.errorText').remove();
     $(error.element).addClass('error');
     $(error.element).after(
       $('<p/>')
  .addClass('errorText')
  .append(error.message)
  );
   $(error.element).after(
       $('<span>There is an issue with this field</span>')
  .addClass('errorIcon')
   );
}
});
},
//rules and messages defined here
);

所以上面的内容没有达到我目前想要的需求,而且感觉我可能使这个问题过于复杂化。我对 javascript 和 Jquery 相当缺乏经验。任何有关对此进行排序的指导将不胜感激。

干杯

编辑:

这是一个jsfiddle的链接:http://jsfiddle.net/WJ9Vt/4/ 附有样本表格。

I am using Jquery validate to provide feedback to a user and provide them updates on the validity of the details they enter in the form. But I am having trouble customising the behaviour Jquery validate creates.

I have a simple form like this:

<form id="form1">
<label for="input1" />
<input name="input1" />
<input type="submit" />
</form>

When the user enters invalid information I want Jquery validate to output something like this:

<form id="form1">
<label for="input1" />
<input name="input1" class="error"/><span class="errorIcon">Error</span>
<p class="errorText">Error message</p>
<input type="submit" />
</form>

When the user fills out the field with valid information I want Jquery validate to output:

<form id="form1">
<label for="input1" />
<input name="input1" /><span class="successIcon">Success</span>
<input type="submit" />
</form>

I have set up the required rules and custom validation messages so they fire fine but I am having trouble getting the behaviour described above.

I have this currently:

$('#form1').validate({
 showErrors: function(errorMap, errorList) {
 if (errorList < 1 ) {
   $('span.errorIcon').remove();
   $('p.errorText').remove();
   $('input.error').removeClass('error');
   $('select.error').removeClass('error');
   return;
 }
 $.each(errorList, function(index, error) {
   if ($(error.element).siblings('.errorText').length === 0 && $(error.element).siblings('.errorIcon').length === 0)  {
     $(error.element).next('p.errorText').remove();
     $(error.element).addClass('error');
     $(error.element).after(
       $('<p/>')
  .addClass('errorText')
  .append(error.message)
  );
   $(error.element).after(
       $('<span>There is an issue with this field</span>')
  .addClass('errorIcon')
   );
}
});
},
//rules and messages defined here
);

So the above doesn't achieve what I want need currently and it also feels like I might be over complicating this issue. I am fairly inexperienced with javascript and Jquery. Any guidance in getting this sorted would be appreciated.

Cheers

EDIT:

Here is a link to a jsfiddle: http://jsfiddle.net/WJ9Vt/4/ with the sample form.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乙白 2024-12-29 06:19:29

还可以发布你的 css 文件,或者对其进行 jsfiddle 吗?因为您的 errorIcon 可能不会显示,因为 只要没有内容或您设置了 display:block; 和特殊的 就不会显示某些内容高度和宽度。

Could also post you css file, or do a jsfiddle on it ? Because you errorIcon is probably not shown because <span> will not display something as long as it has no content or you set display:block; and a special height and width.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文