简体   繁体   English

Ajax 调用成功 Function 触发两次

[英]Ajax Call Success Function Firing Twice

I have an ajax call that gets fired after the onkeyup event of an input.我有一个 ajax 调用,该调用在输入的 onkeyup 事件后被触发。 Once 8 numbers have been entered, it makes a call.一旦输入了 8 个号码,它就会拨打电话。

If the call was successful, it adds a new row to a table.如果调用成功,它将向表中添加一个新行。

The problem is that it's adding 2 rows.问题是它添加了 2 行。 The ajax call if being fired twice as well. ajax 调用也会被触发两次。 If I have the debugger is running, it only adds 1 row.如果我有调试器正在运行,它只会添加 1 行。 If have an alert for when the row is added, it only adds 1 row.如果在添加行时有警报,则仅添加 1 行。 If I don't have the debugger or the alert, it adds 2. Not sure why.如果我没有调试器或警报,它会添加 2。不知道为什么。

The input:输入:

<input style="width:100px" type="text" id="account0" maxlength="8" onkeyup="getAccount(this);" />

The function, addrow is being fired twice (unless I'm debugging or put in a pop-up). function, addrow 被触发两次(除非我正在调试或弹出窗口)。 The addrow function contains no loops, just adds a row to an HTML table. addrow function 不包含循环,只是在 HTML 表中添加一行。 The variable i remains the same for both calls, it does not get incremented:两个调用的变量 i 保持不变,它不会增加:

function getAccount(val) {
        var account = val.value;
        if (account.length == 8) {
            var i = parseInt(val.id.replace("account", ""));
            $.ajax({
                type: "GET",
                url: "/123/AccountSelect?account=" + account,
                dataType: 'json',
                ContentType: "application/json, charset=UTF-8",
                success: function (data) {
                        //create new row for next account if needed
                        addRow(i + 1, data);
                },
                error: function (xhr, status, error) {
                    showModal(xhr.status + ": " + xhr.statusText);
                }
            });
        }
    }

Again, it only does this if there's no debugger running (of course) or no pop-up to interrupt the flow.同样,只有在没有调试器运行(当然)或没有弹出窗口来中断流程时,它才会这样做。

Please try to use async: false as following:请尝试使用async: false如下:

function getAccount(val) {
    var account = val.value;
    if (account.length == 8) {
        var i = parseInt(val.id.replace("account", ""));
        $.ajax({
            type: "GET",
            url: "/123/AccountSelect?account=" + account,
            async: false,
            dataType: 'json',
            ContentType: "application/json, charset=UTF-8",
            success: function (data) {
                    //create new row for next account if needed
                    addRow(i + 1, data);
            },
            error: function (xhr, status, error) {
                showModal(xhr.status + ": " + xhr.statusText);
            }
        });
    }
}

Here's what worked.这是有效的。 I used a boolean to only allow the row to be added once, then used the onmouseup event of the newly created input in the new row to reset the boolean to allow for other rows to be added.我使用 boolean 只允许添加一次行,然后使用新行中新创建的输入的 onmouseup 事件重置 boolean 以允许添加其他行。

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

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