javascript - 使用 live() 进行验证并通过 ajax() 提交

标签 javascript jquery

我在 JQuery 中有这个:

$("form").validate({  rules: {
            captcha: {
                required: true,
                remote: {
        url: "gb_include/captcha.php",
        type: "post"
          },
            }
        },
        messages: {
            captcha: "Correct captcha is required."
        }});
   $("form").submit(function(){

        if($(this).valid() == true){ /* submit via ajax */ }

但是我必须使用.live(),因为表单是在$(document).ready()之后加载的,我该怎么做?

最佳答案

您可以创建一个自定义事件以在加载表单后运行。

$(document).bind('bindForm', function (e) {
    $("#form").validate({  rules: {
        captcha: {
            required: true,
            remote: {
    url: "gb_include/captcha.php",
    type: "post"
      },
        }
    },
    messages: {
        captcha: "Correct captcha is required."
    }});
    $("form").submit(function(){
        if($(this).valid() == true){ /* submit via ajax */ }
    });
});

在加载表单的代码后面添加以下内容:

$(document).trigger('bindForm');

关于javascript - 使用 live() 进行验证并通过 ajax() 提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4359652/

相关文章:

jquery - $(this).find ("li") 和 $ ("li", $(this)) 之间的区别

javascript - 验证用户登录

javascript - 使用 AJAX 时多次触发更改事件

javascript - 通过JavaScript将样式top添加到其他高度元素的ID元素

javascript - ezdz jQuery 插件上传相同图像时不会调用接受

jquery - 在变量中使用 .length > x

javascript - 迭代并追加到表中

javascript - JS 或 CSS - 广告页面 : how to show only adds of certain category?

javascript - getElementsByTagName 似乎不起作用

jquery - 更新 jQuery 版本