Problem with JS / Jquery / Ajax

2

I have a button that when clicked it performs a function:

function searchTradeItems() {
    var uid = $("#useruniqid").val();
    var iusername = $("#username").html();
    var userid = $("#balmung-id").val();
    var itemType = $("#trade-type option:selected").val();
    var valueType = $("#trade-gold-type option:selected").val();
    if (valueType == 5) {
        $("#alert-box").show();
        $("#mensagem-alert").html("Insira o valor da qualidade entre 0 e 100");
        $("#alert-ok").click(function() {
            var iquality = $("#alert-value").val();
            $("#alert-box").fadeOut(200);
            $.ajax({
                url: "systems/action-trade.php",
                type: "POST",
                data: {
                    uid: uid,
                    iusername: iusername,
                    itemType: itemType,
                    valueType: valueType,
                    userid: userid,
                    iname: iname,
                    iquality: iquality
                },
                beforeSend: function() {},
                success: function(result) {
                    $("#user-painel-2").html(result);
                    $("#alert-value").val("");
                }
            });
        });
        $("#alert-cancel").click(function() {
            $("#alert-box").fadeOut(200);
        });
    };
    //----------------------------------------
}

Until then, it picks up the typed data and sends it with ajax, it works, it returns the result that I want perfectly. But the problem is that if I repeat this action it will add up the queries. On the first click it sends an ajax request, if I click again it sends two ajax requests, if I click again it sends 3, and so on ... what would be causing this? He did 6 inquiries at once. Since it was working, I only noticed it after a while.

If it's some silly mistake, I'm sorry, I'm still a beginner.

    
asked by anonymous 14.12.2016 / 04:49

1 answer

3

Declare this function outside the searchTradeItems() function.

$("#alert-ok").click(function() {
    var iquality = $("#alert-value").val();
    $("#alert-box").fadeOut(200);
    $.ajax({
        url: "systems/action-trade.php",
        type: "POST",
        data: {
            uid: uid,
            iusername: iusername,
            itemType: itemType,
            valueType: valueType,
            userid: userid,
            iname: iname,
            iquality: iquality
        },
        beforeSend: function() {},
        success: function(result) {
            $("#user-painel-2").html(result);
            $("#alert-value").val("");
        }
    });
}); 

function searchTradeItems() {
    var uid = $("#useruniqid").val();
    var iusername = $("#username").html();
    var userid = $("#balmung-id").val();
    var itemType = $("#trade-type option:selected").val();
    var valueType = $("#trade-gold-type option:selected").val();
    if (valueType == 5) {
        $("#alert-box").show();
        $("#mensagem-alert").html("Insira o valor da qualidade entre 0 e 100");
        $("#alert-cancel").click(function() {
            $("#alert-box").fadeOut(200);
        });
    };
}
    
14.12.2016 / 12:07