简体   繁体   English

表单提交在AJAX之前完成,没有'alert()'

[英]form submit completes before AJAX without an 'alert()'

I have a page that uses AJAX to call for some html from a php script. 我有一个页面使用AJAX从php脚本中调用一些html。 In that html are many forms of a certain class with unique id's. 在该html中,具有唯一ID的某个类的许多形式。 Upon clicking the submit button the desire is to submit the form to a php script through AJAX so the page doesn't reload or redirect. 单击提交按钮后,希望通过AJAX将表单提交到php脚本,以使页面不会重新加载或重定向。

I have followed the examples of many tutorials ( Here for example ) and they all work... If i throw an alert in the $(document).ready(function(){...}); 我遵循了许多教程的示例( 例如此处 ),它们都可以工作...如果我在$(document).ready(function(){...});发出警报, that wraps them. 包裹他们。 I have attempted at least three or four methods and the result is the always the same. 我尝试了至少三种或四种方法,结果始终是相同的。 Without an alert() in the .ready() the form POSTS to the current page. .ready()没有alert()的形式,POSTS到当前页面。

So what I'm looking for is the fix for needing to throw up an alert() before calling the function that sends the POST since without it the POST is not caught by AJAX and just posts to the current .php file. 因此,我要寻找的是需要在调用发送POST的函数之前抛出alert()的修复程序,因为没有它,POST不会被AJAX捕获,而只会发布到当前的.php文件中。

AJAX AJAX

function formSubmit(form) {

    var element = $(form);
    var id = element.attr("id");
    var form = new FormData($('#' + id)[0]);

    $.ajax({
        type: "POST",
        url: "deleter.php",
        data: form,
        cache: false,
        contentType: false,
        processData: false,

    });
}

$(document).ready(function () {
    alert(''); // Taking this out causes problems

    $('.delete_form').on('submit', function (event) {
        event.preventDefault();
        formSubmit(this);

    });
});

HTML 的HTML

<div class="box">
    <div class="image">
        <a href='.htmlspecialchars($string).'><img src="blank.gif" data-src="'.htmlspecialchars($small).'" width="250" height="376"></a>
    </div>
    <div class="boxOffice">
        <form class="delete_form" id="'.$form_count.'">
            <input  type="hidden" name="delete_link" id="delete_link"value="'.  $string .'" />
            <input  type="submit" name="submit" class="submitButton" id="deleteLinkButton" value=""/>
        </form>
    </div>
</div>

I realize there are about 200 questions related to this... i know because i tried their solutions. 我意识到大约有200个与此相关的问题...我知道,因为我尝试了他们的解决方案。 I'm very new to AJAX ( this is my first project with it ) and I'm out of ways to re-arrange the code with the same result. 我是AJAX的新手(这是我的第一个项目),并且我无法以相同的结果重新安排代码。

If it's needed I'll include the code i use to get the html i included. 如果需要的话,我将包含用于获取包含的html的代码。

-- EDIT -- Someone pointed out that the problem may be other JS on the page interfering with the call etc. So I'm attaching all the JS code on the page exactly how it currently is. - 编辑 -有人指出问题可能出在页面上的其他JS干扰了呼叫等。因此,我将页面上的所有JS代码准确地附加到当前状态。 In this state the POST is executing as expected but as soon as I remove the alert() it doesn't. 在这种状态下,POST会按预期执行,但是一旦我删除alert()它就不会执行。

Interesting note, as I was grabbing the code I moved the getImages() call in and out of the ready() and it does seem to affect if the POST code works correctly. 有趣的是,当我抓取代码时,我将getImages()调用移入了ready()并移出了它,这似乎会影响POST代码是否正常工作。 I have no clue why but it may be a clue. 我不知道为什么,但这可能是一个线索。

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="jquery.unveil.js"></script>

<script>
    $(document).ready(function () {
        alert(''); // Taking this out causes problems


            $('.delete_form').on('submit', function (event) {
                event.preventDefault();

                var element = $(this);
                var id = element.attr("id");
                var form = new FormData($('#' + id)[0]);

                $.ajax({
                    type: "POST",
                    url: "deleter.php",
                    data: form,
                    cache: false,
                    contentType: false,
                    processData: false,

                });
            });

    });

    getImages(); // Moving this inside the .ready(func(){...}) breaks the POST ?? 

    function getImages() {
        $.ajax({
            type: "GET",
            url: "http://localhost/linkler/get_images.php?tag=chubby",
            cache: false,
            timeout: 30000,
            success: function(html){
                $('body').append(html);
                $("img").unveil(); 
            }
        });
        return false;
    }

</script>

Not sure exactly what's the issue, but your js can be simplified: 不确定到底是什么问题,但是可以简化您的js:

$(document).ready(function() {

    $('body').on('submit', '.delete_form', function (ev) {
        ev.preventDefault();
        $.ajax({
              type: "POST",
              url: "deleter.php",
              data: $( this ).serialize(),
              success:function(data){
                   alert(data);
              },
              error:function(data){
                   alert(data);
              },


        });

    });
});

EDIT you are loading the forms via ajax correct? 编辑您通过ajax加载表格正确吗? In which case you must bind your function to the parent element (body). 在这种情况下,您必须将函数绑定到父元素(主体)。 See my syntax change. 请参阅我的语法更改。

Just take this method out of document ready part 只是从准备文档的部分中删除此方法

 $('.delete_form').on('submit', function (event) {
        event.preventDefault();
        formSubmit(this);

    });

I think its about timing. 我认为这与时机有关。 Thats why the "alert('')" cannot be removed . 这就是为什么无法删除“ alert('')”的原因。 Change the script to something like 将脚本更改为类似

function delete_form_init(){
 $('.delete_form').on('submit', function (event) {
    event.preventDefault();
    formSubmit(this);
    }



function formSubmit(form) {

var element = $(form);
var id = element.attr("id");
var form = new FormData($('#' + id)[0]);

$.ajax({
    type: "POST",
    url: "deleter.php",
    data: form,
    cache: false,
    contentType: false,
    processData: false,

});
}

$(document).ready(function () {
   delete_form_init();

});

}); });

This way the function delete_form_init will be insert into the model before the .ready calls it 这样,函数delete_form_init将在.ready调用之前插入模型中

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

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