简体   繁体   English

Ajax通话进行了多次

[英]Ajax calls going multiple times

I have written a code in Javascript in which I have attached an input type submit to a form . 我已经用Javascript编写了代码,其中将输入类型提交提交到了form On form submit the listener gets called. 在提交表单时,将调用侦听器。

The problem is that on when I click the button once, one ajax call occurs. 问题是,当我单击按钮一次时,就会发生一个ajax调用。 When I click it again two calls occur while only one call should occur on each click. 当我再次单击它时,将发生两个呼叫,而每次单击仅应发生一个呼叫。 Similarly on clicking 3 times 3 calls occur and so on...(the calls get increasing). 同样,单击3次会出现3个呼叫,依此类推...(呼叫数量增加)。 If I refresh the page then the number gets reset. 如果刷新页面,则该数字将重置。 I have tried everything but I had no luck. 我已经尝试了一切,但没有运气。 If anyone found out what is wrong here it would be awesome. 如果有人发现这里出了问题,那将很棒。 Thanks in advance. 提前致谢。

javascript code: JavaScript代码:

$('input.create-discounts-quotations').click(function () {

    var discount_quotation_type = $('input.quotation-discount-type').val();

    if (discount_quotation_type == "value") {

        var total = $('input.discount-input-quotation').val();
        var discounted_price = product_price - total;
        $('#final_discounted_amount').val(discounted_price);

        $("table.product-response-table tr").each(function () {
            var row = $(this).index() + 1;
            var td = $(this).find('td.quotation-response-discounts');

            $(td).each(function () {
                $(this).html(total);
            });
        });
        $("table.product-response-table tr").each(function () {
            var row = $(this).index() + 1;
            var td = $(this).find('td.product_final_price_discounted');

            $(td).each(function () {
                $(this).html(discounted_price);
            });
        });
        var form1 = $('form#quotation_discount_update_form');
        form1.on("submit", function (e) {
            var form_data1 = form1.serialize();
            $.ajax({
                type: 'POST',
                url: form1.attr('action'),
                data: form_data1,
                dataType: "json",
                success: function (data) {
                    $('.quotation-discount-status-update').empty();
                    $('.quotation-discount-status-update').append('<div class="alert alert-success">Discount Added</div>');
                }
            });

            e.preventDefault();
        });
    }
    if (discount_quotation_type == "percentage") {
        var total = $('input.discount-input-quotation').val();
        var temp_first = product_price;
        var temp1 = total / 100;
        var temp2 = temp1 * product_price;
        var discounted_price = product_price - temp2;
        $('#final_discounted_amount').val(discounted_price);

        $("table.product-response-table tr").each(function () {
            var row = $(this).index() + 1;
            var td = $(this).find('td.quotation-response-discounts');

            $(td).each(function () {
                $(this).html(total);
            });
        });
        $("table.product-response-table tr").each(function () {
            var row = $(this).index() + 1;
            var td = $(this).find('td.product_final_price_discounted');

            $(td).each(function () {
                $(this).html(discounted_price);
            });
        });
        var form1 = $('form#quotation_discount_update_form');
        form1.on("submit", function (e) {
            var form_data1 = form1.serialize();
            $.ajax({
                type: 'POST',
                url: form1.attr('action'),
                data: form_data1,
                dataType: "json",
                success: function (data) {
                    $('.quotation-discount-status-update').empty();
                    $('.quotation-discount-status-update').append('<div class="alert alert-success">Discount Added</div>');
                }
            });

            e.preventDefault();
        });
    }
    if (discount_quotation_type == "not_selected") {
        $('.quotation-discount-status-update').empty();
        $('.quotation-discount-status-update').append('<div class="alert alert-danger">Discount Method Not Selected</div>');
        return false;
    }
   //        return false;
});

That happen because every time you click your code will reattach the submit event so it will be duplicated in every click. 这种情况发生,因为每次你点击你的代码将重新将submit事件,因此它会在每一次点击复制。

You should never attach the events inside other events, please put the submit event outside of the click event and the code should work, example : 您永远不要将事件附加在其他事件中,请将submit事件放在click事件之外,代码应该可以工作,例如:

var form1 = $('form#quotation_discount_update_form');
form1.on("submit", function (e) {
    var form_data1 = form1.serialize();

    $.ajax({
        type: 'POST',
        url: form1.attr('action'),
        data: form_data1,
        dataType: "json",
        success: function (data) {
            $('.quotation-discount-status-update').empty();
            $('.quotation-discount-status-update').append('<div class="alert alert-success">Discount Added</div>');
        }
    });

    e.preventDefault();
});

Else you have to remove the event handler every time using .off() , like : 否则,您每次必须使用.off()删除事件处理程序,例如:

form1.off("submit").on("submit", function (e) {

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

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