2015-10-06 19 views
1

我正在使用swal(http://t4t5.github.io/sweetalert)來獲取用戶點擊某些內容時的某些數據。然後,我想從我調用swal的函數中返回。換句話說,對於下面的示例,我想將labels中項目的文本設置爲輸入值。問題是,它似乎返回SwaI位已被關閉/數據已輸入前:sweetalert阻止像正常提示

.on('dblclick', function(l) { 
    labels.text(function (e) { 
    return swal({ 
     title: "Edit label" 
    }, 
     function(inputValue){ 
     if(inputValue) { 
     return inputValue 
     } 
    }); 
    }) 
}); 

正常prompt alert阻止這樣可以做到這一點,可以swal做到這一點?

感謝

回答

2

雖然你不能有Sweet Alerts塊,但你可以做的就是使用它的回調函數來觸發任何需要警報的代碼。這些例子有幾個例子。例如,假設你有一個是/否風格的警報,那就是文件刪除的例子。

swal({ 
    title: "Are you sure?", 
    text: "You will not be able to recover this imaginary file!", 
    type: "warning", 
    showCancelButton: true, 
    confirmButtonColor: "#DD6B55", 
    confirmButtonText: "Yes, delete it!", 
    cancelButtonText: "No, cancel plx!", 
    closeOnConfirm: false, 
    closeOnCancel: false 
}, 
function(isConfirm){ 
    //The callback will only fire on cancel if the callback function accepts an 
    //argument. So, if the first line were 'function() {' with no argument, clicking 
    //cancel would not fire the callback. 
    if (isConfirm) { 
    swal("Deleted!", "Your imaginary file has been deleted.", "success"); 
    } else { 
    swal("Cancelled", "Your imaginary file is safe :)", "error"); 
    } 
}); 

還是AJAX例如:

swal({ 
    title: "Ajax request example", 
    text: "Submit to run ajax request", 
    type: "info", 
    showCancelButton: true, 
    closeOnConfirm: false, 
    showLoaderOnConfirm: true, 
}, 
function(){ 
    setTimeout(function(){ 
    swal("Ajax request finished!"); 
    }, 2000); 
}); 

在這兩種,回調將不會觸發,直到警報與互動,以及呼叫的結果傳遞中作爲參數傳遞給回調。

所以說,你需要等到有人點擊好吧。

swal({ 
    title: "Delete your account?", 
    text: "Clicking on continue will permanently delete your account.", 
    type: "warning", 
    confirmButtonText: "Continue", 
    closeOnConfirm: false 
}, function() { 
    swal("Deleted account", "We'll miss you!", "success"); 
}); 

注:closeOnConfirm/closeOnCancel僅需要false如果你在顯示回調的後續警報。如果它設置爲true,它將在向用戶顯示之前關閉第二個警報。但是,如果你正在做一些與非swal相關的事情,並且你沒有關閉它,它將無限期地保持打開狀態。

swal({ 
    title: "Delete your account?", 
    text: "Clicking on continue will permanently delete your account.", 
    type: "warning", 
    confirmButtonText: "Continue" 
}, function() { 
    console.log("This still shows!") 
}); 

如果您希望告警,而你正在做一些非swal相關的繼續開放,你應該在你的代碼的末尾調用swal.close()

swal({ 
    title: "Delete your account?", 
    text: "Clicking on continue will permanently delete your account.", 
    type: "warning", 
    confirmButtonText: "Continue", 
    closeOnConfirm: false 
}, function() { 
    console.log("This still shows!"); 
    setTimeout(function() { 
    // This will close the alert 500ms after the callback fires. 
    swal.close(); 
    }, 500); 
}); 
+1

把一切都放在回調中。我不知道只有原生功能可以阻止。有趣。 +1的詳細答案,儘管從文檔複製 – amlwwalker

+0

@amlwwalker是的。我試圖在我的回答中包括文檔的相關部分,以及最後的一些自定義示例。 :)還有一個名爲[wait.for](https://github.com/luciotato/waitfor)的庫,儘管我對它不熟悉,所以我不能說它在你的用例中的有效性。 –

1

不,不可能,也沒有辦法阻止創建代碼,等待用戶輸入。只有JS已經提供了一些方法:提示,提醒等等。只需從回調中運行你需要的代碼即可。