0

我正在使用 jquery galleryview 插件,并且添加了在单击链接时暂停幻灯片(显示表单)然后在单击关闭按钮时重新启动的功能。

这工作正常(见下文),但我也希望幻灯片在提交表单时开始。我可以通过将提交按钮添加到重新启动幻灯片的元素列表来做到这一点,但是如果表单没有通过验证,我不希望幻灯片重新启动。

这是我的galleryview代码(精简):

(function($){
    $.fn.galleryView = function(options) {
    // Some code
        return this.each(function() {
        // Some more code
            $("#closeEnquiry").click(function(){
                restartGallery();
            });

            function restartGallery(e) {
                // Code to restart gallery
            }
        }
    }
}

这是我的表单提交/验证代码(restartGallery 函数不运行):

$(document).ready(function(){
    $("#enquiryForm").validate({
        debug: false,
        rules: {
            YourName: {
                required: true,
            },
            YourEmail: {
                required: true,
                email: true,
            },
        },
        messages: {
            YourName: "Please let us know who you are.",
            YourEmail: "A valid email will help us get in touch with you."
        },
        submitHandler: function(form) {

            $.post('projectform.php', $("#enquiryForm").serialize(), function(data) {
                closePopup();
                $('#enquiryFormContainer').prepend(data);
                restartGallery();  // This does not run...
            });

        }
    });
}); 

有什么想法可以从 validate 函数运行 restartGallery 函数吗?

4

1 回答 1

0

I had a similar problem once where JQuery objects were becoming invalid (disposed) after changing the DOM through AJAX.

Therefore, my guess is that after doing $('#enquiryFormContainer').prepend(data), some DOM objects (and their event handlers) referenced by restartGallery() are becoming invalid. For example, if restartGallery() references a variable that was initialized like this: var myDiv = $("#myDiv"), then you will need to re-initialize it after the AJAX call finishes.

于 2012-03-02T16:06:25.567 回答