简体   繁体   English

无法停止打开Action目标来提交AJAX表单

[英]Can't stop AJAX form submission from opening Action target

I basically copied an entire form submission code I had up and running on a Website and pasted it into a blank file in order to modify its contents and save some time. 我基本上复制了我在网站上运行的完整表单提交代码,然后将其粘贴到空白文件中,以修改其内容并节省一些时间。 IN THEORY, then, if the original file stopped normal form submission from opening up the Action target file while still completing the submission, this one should do the same. 从理论上讲,如果原始文件在继续完成提交的同时停止了打开动作目标文件的正常表单提交,则该文件应该执行相同的操作。

However, I can't get it to work like it did. 但是,我无法像它那样正常工作。 I can't stop the submission from leaving the current window. 我无法阻止提交内容离开当前窗口。

Can anyone please point me toward what I'm doing wrong? 谁能指出我做错了什么?

(Aside from the PHP code I'm using the jQuery Validation Plugin, same as I was using in the previous form that worked as expected.) (除了PHP代码之外,我正在使用jQuery Validation插件,与我在以前的表格中使用的一样,可以正常工作。)

Form HTML code: 表单HTML代码:

<form id="form" class="appintro" method="post" action="manageform.php" enctype="multipart/form-data"></form>

JS JS

$.validator.setDefaults({
    submitHandler: function() { //<-- Handler called by jQuery Validation once form is validated
        $('.msg.sending').fadeIn(200);
        $.ajax({
            type: 'POST',
            url: form.attr('action'),
            data: form.serialize(),
            success: function() {
                alert('Success')
    },
            error: function() {
                alert('Failure')
            }
        });
        return false; //<-- This should stop the normal submission...
    }
});

I've also already tried calling the AJAX outside of the validator code, ie using $("form").submit(function(event)... , etc. Also tried setting data to form.serializeArray() , as recommended in some other SO post... Nothing does it. 我也已经尝试过在validator代码之外调用AJAX,即使用$("form").submit(function(event)...等。)还尝试将data设置为form.serializeArray() ,如推荐的那样。其他一些帖子...什么也没做。

Thanks in advance for your help! 在此先感谢您的帮助!

EDIT 1 编辑1

I've set up this jsFiddle to test it out in a simpler version. 我已经设置了这个jsFiddle以在一个简单的版本中对其进行测试。 No matter what I place in AJAX's url , I get an error. 无论我在AJAX的url放置什么,都会收到错误消息。 If I fill the form's action , then I can't catch the submission. 如果我填写表单的action ,那么我将无法捕获提交内容。

Edit 2 Ok while fixing some bugs in my version of your js fiddle, I figured what the issue is. 编辑2好的,同时修复了我的js小提琴版本中的一些错误,我发现了问题所在。

This line is missing the form parameter 该行缺少form参数

submitHandler: function() { 

It should look like this: 它看起来应该像这样:

submitHandler: function(form) { 

Next, to call serialize, you need to wrap make it a jquery object. 接下来,要调用序列化,您需要将其包装为一个jquery对象。 The form passed in by jquery validate is just a regular form object and not a jquery one. jquery validate传递的表单只是一个常规表单对象,而不是jquery一个。 So you need to do this. 因此,您需要执行此操作。

data: $(form).serialize(),

If you call form.serialize , you should get this error in Chrome: Uncaught TypeError: Object #<HTMLFormElement> has no method 'serialize' , which could explain why your browser is reloading. 如果调用form.serialize ,则应在Chrome中出现以下错误: Uncaught TypeError: Object #<HTMLFormElement> has no method 'serialize' ,这可以解释为什么重新加载浏览器。

Edit 1 I looked at your fiddle and I found some bugs, but I'm not sure they fix your problem. 编辑1我看了你的小提琴,发现了一些错误,但是我不确定它们是否可以解决您的问题。 I feel like I just fixed some errors specific to jsfiddle. 我觉得我只是修复了一些特定于jsfiddle的错误。

Here is a link to an updated fiddle that works: http://jsfiddle.net/JSuUL/6/ 这是一个有效的更新小提琴的链接: http : //jsfiddle.net/JSuUL/6/

Here is the code with some annotations 这是带有一些注释的代码

$.validator.setDefaults({
    // Need to pass in form variable
    submitHandler: function (form) {
        alert('Sending...')
        $.ajax({
            type: 'POST',

            // First off changed this to make the request work
            // http://doc.jsfiddle.net/use/echo.html
            url: '/echo/html/',

            // Instead of form, I used $(form) to reference the form as a jquery object.
            data: $(form).serialize(),

            success: function () {
                alert('Success!')
            },
            error: function () {
                alert('Failure!')
            }
        });
        return false;
    }
});

$(document).ready(function () {
    // I added a "#" here so we can grab the form. Your jsfiddle had $(form)
    $("#form").validate({
        rules: {
            name: {
                required: true,
                minlength: 2
            },
            surname: {
                required: true,
                minlength: 2
            },
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM