我在這裏有一個代碼將數據插入到數據庫中,如果數據已經存在,它將首先進行過濾,並詢問用戶是否要繼續使用引導模式插入數據。現在問題是它只顯示在循環結束時,我想要的是每次在數據庫中找到一個彈出窗口模式將顯示的數據。如何在使用jquery的循環中調用bootstrap模式
$.each(person, function(index, value){
var existing = DisticntVal(value);
if(existing == 0){
InsertPerson(value);
}else{
var a = ConfirmYesNo(value['person'] + " already exist. Do you want to continue?");
a.then(function (b) {
if(b == 1){
InsertPerson(value);
}
});
}
});
function ConfirmYesNo(msg) {
var dfd = jQuery.Deferred();
var $confirm = $('#exampleModal');
$confirm.modal('show');
$('#message').html(msg);
$('#btnyes').off('click').click(function() {
$confirm.modal('hide');
dfd.resolve(1);
return 1;
});
$('#btnno').off('click').click(function() {
$confirm.modal('hide');
return 0;
});
return dfd.promise();
}
function DisticntVal(person) {
var returncount;
var nperson = JSON.stringify(person);
$.ajax({
url: 'chckdistinct.php',
type: 'post',
data: {person: nperson},
async: false,
success: function(response) {
returncount = response;
},
error: function(xhr, desc, err) {
console.log(xhr);
console.log("Details: " + desc + "\nError:" + err);
}
});
return returncount;
}
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p id="message"></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal" id="btnno">No</button>
<button type="button" class="btn btn-primary" id="btnyes">Yes</button>
</div>
</div>
</div>
</div>
需要幫助的這一個。
「這隻能說明在循環的結束」。好,但由於我們無法看到你的數據,而且我們也看不到你的數據,而且我們也看不到'DisticntVal' _ [sic] _函數的作用,所以我們不能確定這是否是正確的行爲。 – ADyson
我想要做的就像javascript的confimr()函數,它暫停循環,直到按鈕事件已經點擊,然後繼續。 – JRM
ConfirmYesNo不會暫停執行,它只是使HTML(模態)的那一點可見,然後繼續。因此,在用戶有機會迴應模態之前,循環會一次又一次地繼續,但只是用最新內容覆蓋模態內容和事件處理程序。舊的承諾等從未得到解決。另一條建議:'async:false'是一個壞主意。它會導致不良的用戶體驗(瀏覽器在ajax請求期間被鎖定,並且沒有任何內容可以被點擊),並且已經被棄用,所以瀏覽器將來可能會刪除該功能。 – ADyson