簡體   English   中英

等待按鈕單擊事件,然后繼續循環Javascript

[英]Wait for button click event before continue loop Javascript

我有一個代碼循環,將選項添加到彈出模式,每個選項旁邊都有按鈕。

在搜索循環中,有時當搜索返回多個項目時,我們必須顯示模態,然后用戶選擇哪個部分。

我想暫停循環並等待用戶選擇一個項目,然后再繼續。

我的圈

$.each(rows, function (index, item) {
    SearchItems(item.split('\t')[0], item.split('\t')[1]);
});

搜索項目功能

function SearchItems(searchedPart, quantity) {

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "OrderFormServices.asmx/GetItemInfoAdmin",
        data: "{'itemname':'" + searchedPart + "','quantity':'" + (quantity || 1) + "', 'guid':'" + custGuid + "' }",
        dataType: "json",
        success: function (result) {

            result = result.d;

            if (result.length > 1) {

                var htmlString = "";
                $.each(result, function(index, item) {
                    item.PartNumber = (item.PartNumber != "") ? item.PartNumber : item.ItemName;
                    htmlString += "<tr><td>"
                        + "<input type=\"hidden\" name=\"ItemID\" value=\"" + item.ItemID + "\">"
                        + "<input type=\"hidden\" name=\"ItemCode\" value=\"" + item.ItemCode + "\">"
                        + "<input type=\"hidden\" name=\"Price\" value=\"" + item.Price + "\">"
                        + "<input type=\"hidden\" name=\"Quantity\" value=\"" + item.Quantity + "\">"
                        + "<input type=\"hidden\" name=\"CustomerReference\" value=\"" + searchedPart + "\">"
                        + "<input type=\"hidden\" name=\"PackQuantity\" value=\"" + item.PackQuantity + "\">"
                        + "<input type=\"hidden\" name=\"FreeStock\" value=\"" + item.FreeStock + "\">"
                        + item.PartNumber + "</td><td>"
                        + item.ManufacturerName + "</td><td>"
                        + item.ItemName + "</td><td>"
                        + item.ItemDescription + "</td><td><input type='button' name=\"selectPopup\" onclick=\"ChoosePopup($(this).closest('tr'));\" class='btn  btn-primary btn-xs' value='Select'></td></tr>";
                });
                $("#tbodyPopupContent").html(htmlString);
                $("#PopUpNormalProduct").modal();
                modalfix();
                $("#divPopupWindow").parent("").addClass("quicksearchpopup");

//////////////////////////////////////////////////////////////////////////
//////////////// WAIT FOR BUTTON CLICK AND PERFORM CODE///////////////////
//////////////////////////////////////////////////////////////////////////

                $("#partNumber").prop("disabled", false);

            } else if (result.length < 1) {
                $("#partNumber").prop("disabled", false);
                $('#partNumber').focus();
            }
            else {
                ///////
            }
        }
    });
}

彈出項按鈕會觸發ChoosePopup()函數。

function ChoosePopup(row) {

    var $hidden_fields = row.find("input:hidden");
    var $tds = row.find("td");

    var newItem = {
        ItemID: parseFloat($hidden_fields.eq(0).val()),
        ItemCode: $hidden_fields.eq(1).val(),
        ItemDescription: $tds.eq(3).text(),
        ItemName: $tds.eq(2).text(),
        Price: parseFloat($hidden_fields.eq(2).val()),
        Quantity: parseFloat($hidden_fields.eq(3).val()),
        CustomerReference: $hidden_fields.eq(4).val(),
        PackQuantity: parseFloat($hidden_fields.eq(5).val()),
        FreeStock: parseFloat($hidden_fields.eq(6).val())
    };

    AddNewRow(newItem, true);
    $("#PopUpNormalProduct").modal("hide");
}

在繼續循環之前,如何等待ChoosePopup()函數完成?

定義全局變量,並根據彈出窗口的要求,將其余代碼移至ChoosePopup()函數。

我認為您應該重新考慮解決問題的方式。 您應該使用回調觸發ChoosePopup函數,以根據用戶輸入完成其余工作。

您可能考慮將rows數組傳遞給SearchItems函數,以便可以設置回調以遍歷行中的其余項。

這是一個不需要重復代碼的示例。

 const items = [1, 2, 3, 4, 5] //main function which writes buttons from an array function makeButtons(array) { //copy the array so we can keep track of which items are left to process after click event var unProcItems = array.slice() $.each(array, function(index, item) { var exit = false //remove the item from the processed array unProcItems.splice(unProcItems.indexOf(item), 1) //make our button var button = document.createElement('button') button.innerHTML = `Button ${item}` //if something then hook up a click event with callback if (item == 3) { button.onclick = function(event) { //call our makeButtons function in the callback passing in the unprocessed items array makeButtons(unProcItems) //remove the click event so it can't be fired again event.target.onclick = null } exit = true } document.getElementById('content').append(button) //exit the loop if our flag was set if (exit) return false; }) } makeButtons(items); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="content"> </div> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM