jQuery Validator插件-从函数内部触发 [英] jQuery Validator Plug In - trigger from within a function

查看:90
本文介绍了jQuery Validator插件-从函数内部触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

加载文档后,我无法验证通过AJAX加载的长格式.使用标准的验证语法,验证器会在文档存在之前在表单中查找我的表单,因此会给我一个错误:

I am having trouble validating a long form that is loaded via AJAX after the document is loaded. Using the standard validation syntax, the validator looks for my form in the document before it exists and therefore gives me an error:

$(document).ready(function(){
   $("#mainForm").validate();
});

firebug回应:

firebug responds with:

nothing selected, can't validate, returning nothing

我尝试将$("mainForm").validate();放入函数中,然后使用表单中的onSubmit事件调用该函数,但没有运气:

I tried putting the $("mainForm").validate(); in a function then calling the function with the onSubmit event from the form but no luck:

function validate() {
  $("mainForm").validate();
};

----------

<form id="mainForm" onSubmit="validate();">
   ...
</form>

有想法吗?

@Chris的一些其他信息:

Some additional info for @Chris:

我有一个页面正在根据许多不同的模块动态创建表单.用户选择适用于他们的模块,然后用该信息进行更新以填写表单.因此,当用户单击链接加载模块时,将调用loadSection();函数.这是loadSection();函数的样子:

I have a page that is dynamically creating a form based on many different modules. The user picks the module(s) that apply to them then the form updates with that information to fill in. So when the user clicks on a link to load a module the loadSection(); function is called. This is what the loadSection(); function looks like:

function loadSection(id, div, size, frame) {
  var url = "loadSection.php?id=" + id + "&size=" + size + "$frame=" + frame;
  $.get(url,
    function(data){
      $(div).append(data);
    });
}

如果我输入`$(#mainForm).validate();'在此函数的回调中,每次插入新模块时都有可能被调用.我不确定这是好是坏,即使在表单的字段已更改的情况下,我也不确定在同一表单上如何多次调用验证.

If I put the `$(#mainForm).validate();' in the callback of this function, it has the potential to get called every time a new module is inserted. Which may be good, or may be bad, I'm not sure how the validation will take to be called multiple times on the same form, even if the fields of the form have changed.

有想法吗?

推荐答案

您可能已经正确解决了该问题.如果该表单不存在于document.ready()的DOM中,则将不会绑定任何内容.

You've likely got the problem right. If the form doesn't exist in the DOM at document.ready(), then nothing will be bound.

由于您使用的是JQuery,因此我假定使用jquery $ .ajax(或类似功能)添加表单.最直接的解决方案是将$("#mainForm").validate();添加到AJAX请求的回调函数中.如果您不使用JQUery ajax,请发布添加表单的代码,我们将为您提供进一步的帮助.

Since you're using JQuery, I presume the form is added using the jquery $.ajax (or similar) function. The most straightforward solution would be just to add $("#mainForm").validate(); to the callback function of the AJAX request. If you're not using JQUery ajax, please post the code that's adding the form, and we can help you out further.

这篇关于jQuery Validator插件-从函数内部触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆