ここにはデータを挿入するコードがあります。データがすでに存在する場合は最初にフィルターをかけ、ブートストラップ・モーダルを使用してデータを挿入し続けるかどうかを尋ねます。今問題は、ループの最後にのみ表示され、私はポップアップモーダルが表示されますデータベース上のデータが検出されるたびに私が望むものです。jqueryを使用してループ内でブートストラップ・モーダルを呼び出す方法
$.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>
は、この1とが必要です。
"それだけでループの最後に示して"。しかし、私たちはあなたのデータを見ることができず、 'DisticntVal' _ [sic] _関数が何をしているのか分からないので、実際に正しい振る舞いであるかどうかは実際には言えません。 – ADyson
私がしようとしているのはボタンイベントがクリックされてから続けられるまでループを一時停止するjavascriptのconfimr()関数のようなものです。 – JRM
ConfirmYesNoは実行を一時停止せず、そのままHTML(モーダル)のビットを表示してから続行します。したがって、ユーザーがモーダルに応答する前に、ループは何度も繰り返されますが、最新の内容でモーダルコンテンツとイベントハンドラーを上書きします。古い約束などは決して解決されません。別のアドバイス: 'async:false'は悪い考えです。それは悪いユーザエクスペリエンスを引き起こします(ブラウザはajaxリクエストの間ロックされ、何もクリックすることはできません)。また、既に非推奨になっています。 – ADyson