jQuery表单验证不适用于新创建的元素 [英] JQuery Form Validation Not working on newly created elements

查看:103
本文介绍了jQuery表单验证不适用于新创建的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Jquery Validation插件时遇到了一些问题,想知道是否有人可以提供帮助.

Am having a few issues with the Jquery Validation plug in, and wondering if anyone can assist.

目前,该插件可与页面上当前使用的任何表单元素一起使用

At the moment, the plug in works with any form elements currently on the page

    $("#addRelease").validate({
     submitHandler: function(form) {
     form.submit();
     }
   });

但是,如果我在单击按钮时动态创建表单,则Jquery Validation插件将不适用于此新创建的元素.

However if I dynamically create a form on a button click, the Jquery Validation plug in will not work for this newly created element.

    // Add release form
$(function(){
$('body#true #releases p a').click(function(){
         // Grab form using Jquery
        $.get('http://localhost:8500/mxRestore/views/viewlets/forms/ajax/a_addRelease.cfm?name_art='+name_art, function(data) { 
        // Place form returned via Jquery on page in formWrapper div        
        $('#formWrapper').html(data);
    });

    return false 
    });


});

以前有人遇到过这个问题并且知道可以解决吗?

Has anyone run into this issue previously and know of a work around?

非常感谢

推荐答案

很难告诉您看不到您的代码,但是在将html插入页面后,您可能需要绑定"validate()"方法,因为#直到用户启动ajax调用(通过单击),然后再将该元素添加到页面中,jQuery才能找到addRelease元素.

Hard to tell without seeing your code, but you might need to bind the "validate()" method after you have inserted the html into your page because the #addRelease element can't be found by jQuery until the user initiates the ajax call (by clicking) which then adds that element to the page.

// Add release form
$(function(){
    $('body#true #releases p a').click(function(){
        // Grab form using Jquery
        $.get('http://localhost:8500/mxRestore/views/viewlets/forms/ajax/a_addRelease.cfm?name_art='+name_art, function(data) { 
            // Place form returned via Jquery on page in formWrapper div        
            $('#formWrapper').html(data);
            $("#addRelease").validate({
                submitHandler: function(form) {
                    form.submit();
                  }
            });
        });
        return false;
    });
});

这篇关于jQuery表单验证不适用于新创建的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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