简体   繁体   English

在一个链接上同时单击两个ajax发布jquery

[英]Two ajax post simultaneously on one link click jquery

I am using one link which has class name next and id end . 我正在使用一个链接,链接的类名称为next ,id为end

On clcik on it both class name and id i am using jquery post. 关于clcik它的类名和ID我正在使用jquery post。

The issue i am getting is sometimes the ajax request fires multiple times on one click.on one click i am getting data from one url and simultaneously saving these data into db by another url.So sometimes there are some issues coming while inserting into db.sometimes null values enters and sometimes multiple rows entering into db.So how can i write these two functions so that both will work perfectly? 我遇到的问题是,有时ajax请求会一键触发多次。一键单击时,我会从一个URL获取数据,并同时通过另一个URL将这些数据保存到db中。因此有时在插入db时会遇到一些问题。有时会输入null值,有时会向db中输入多行。那么我该如何编写这两个函数,以便两者都能正常工作?

$('.next').live('click', function (e) {
    e.preventDefault();
    var result = [];
    var answer = [];
    var le = '';
    $('.answertext').each(function (index, element) {
        result.push($(this).val());
    });

    $('.answer').each(function (index, element) {
        answer.push($(this).val());
    });
    le = $('#level').val();
    mle = $('#mainlevel').val();
    $.ajax({
        url: 'matchanswers.php',
        type: 'POST',
        data: {
            result: result,
            answer: answer,
            level: le,
            mle: mle
        },
        async: true,
        beforeSend: function () {
            // show indicator
        },
        complete: function () {
            // hide indicator
        },
        success: function (data) {
            $('.quizform').html(data);
        }
    });
});

$('#end').live('click', function (e) {
    e.preventDefault();
    var sublev = $('#level').val();
    var score = $('#count').val();
    if (sublev < 11) {
        $.ajax({
            url: 'submitanswers.php',
            type: 'POST',
            data: {
                sublev: sublev,
                score: score
            },
            async: true,
            beforeSend: function () {
                // show indicator
            },
            complete: function () {
                // hide indicator
            },
            success: function (data2) {}
        });
    } else {
        $.ajax({
            url: 'getanswers.php',
            type: 'POST',
            data: {
                sublev: sublev,
                score: score
            },
            async: true,
            beforeSend: function () {
                // show indicator
            },
            complete: function () {
                // hide indicator
            },
            success: function (data3) {
                if (data3) {
                    $('.quizform').html("");
                    $('form :input').attr('disabled', 'disabled');
                    $('#logout').removeAttr("disabled");
                    var obj = $.parseJSON(data3);
                    $('#sum').html("Your Total Score for level - " + obj[0] + " is " + obj[1] + " in " + obj[2] + "secs");
                }
            }
        });
    }
});

Simply check for the event trigger like : 只需检查事件触发器,例如:

$('.next').live('click', function (e) {
    if(e.handled !== true){ // This will prevent event triggering more then once
        e.handled = true;
        //Your code

    }
});



$('#end').live('click', function (e) {
    if(e.handled !== true){ // This will prevent event triggering more then once
        e.handled = true;
        //Your code

    }
});

By doing so, you will stop multiple event trigger which is quite a common problem and should solve your problem. 这样,您将停止多个事件触发,这是一个很常见的问题,应该可以解决您的问题。

Edit : 编辑:

Your full code will be : 您的完整代码为:

$('.next').live('click', function (e) {
    if (e.handled !== true) { // This will prevent event triggering more then once
        e.handled = true;
        //Your code

        e.preventDefault();
        var result = [];
        var answer = [];
        var le = '';
        $('.answertext').each(function (index, element) {
            result.push($(this).val());
        });

        $('.answer').each(function (index, element) {
            answer.push($(this).val());
        });
        le = $('#level').val();
        mle = $('#mainlevel').val();
        $.ajax({
            url: 'matchanswers.php',
            type: 'POST',
            data: {
                result: result,
                answer: answer,
                level: le,
                mle: mle
            },
            async: true,
            beforeSend: function () {
                // show indicator
            },
            complete: function () {
                // hide indicator
            },
            success: function (data) {
                $('.quizform').html(data);
            }
        });

    }
});



$('#end').live('click', function (e) {
    if (e.handled !== true) { // This will prevent event triggering more then once
        e.handled = true;
        //Your code

        e.preventDefault();
        var sublev = $('#level').val();
        var score = $('#count').val();
        if (sublev < 11) {
            $.ajax({
                url: 'submitanswers.php',
                type: 'POST',
                data: {
                    sublev: sublev,
                    score: score
                },
                async: true,
                beforeSend: function () {
                    // show indicator
                },
                complete: function () {
                    // hide indicator
                },
                success: function (data2) {}
            });
        } else {
            $.ajax({
                url: 'getanswers.php',
                type: 'POST',
                data: {
                    sublev: sublev,
                    score: score
                },
                async: true,
                beforeSend: function () {
                    // show indicator
                },
                complete: function () {
                    // hide indicator
                },
                success: function (data3) {
                    if (data3) {
                        $('.quizform').html("");
                        $('form :input').attr('disabled', 'disabled');
                        $('#logout').removeAttr("disabled");
                        var obj = $.parseJSON(data3);
                        $('#sum').html("Your Total Score for level - " + obj[0] + " is " + obj[1] + " in " + obj[2] + "secs");
                    }
                }
            });
        }

    }
});

You are firing click on same click even if id and class are different the link is same. 即使ID和类别不同,您也要触发相同的点击,链接是相同的。

 $('.next').live('click', function(e) 

fires one ajax call and 触发一个ajax调用,

$('#end').live('click', function(e)

fires another, what you can do is fire one ajax on success of other 触发另一个,您可以做的就是在另一个成功的情况下触发一个ajax

$('.next').live('click', function(e)  { ...
     success: function(data) { $.ajax({
                url: 'submitanswers.php', }

but this is not good practice 但这不是一个好习惯

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

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