AJAX を使用してユーザーをアクティブ化および非アクティブ化するためのチェックボックス (Bootstrap Switch ライブラリを使用してオン/オフ トグルとして機能する) があります。
ユーザーがボックスのチェックを外すと、この関数が起動されます:
$('.user-status-ckbx').on('switchChange.bootstrapSwitch'...
confirm() ダイアログがポップアップし、ユーザーを非アクティブ化/アクティブ化するかどうかをユーザーに尋ねます。ユーザーが「いいえ」をクリックすると、ボタンは以下を使用して元の状態に戻されます。
$("#" + e.currentTarget.id).bootstrapSwitch('toggleState');
私が抱えている問題は、toggleState() が実行されるたびに、switchChange.bootstrapSwitch も再度実行されることです。これにより、ユーザーがメッセージを確認した場合にのみ消える、終わりのない confirm() メッセージが送信されます。
実際のユーザーのクリックとプログラムで生成されたトグルに基づいて、switchChange.bootstrapSwitch メソッドが実行されないようにする効率的な方法はありますか?
私はすでに試しました:
e.originalEvent !== undefined
と
e.which
他の同様の質問で示唆されているように、それらのどれも機能せず、「e」オブジェクトにも表示されません...
<script>
$(".user-status-ckbx").bootstrapSwitch('size', 'mini');
$(".user-status-ckbx").bootstrapSwitch('onText', 'I');
$(".user-status-ckbx").bootstrapSwitch('offText', 'O');
//ajax to activate/deactivate user
$('.user-status-ckbx').on('switchChange.bootstrapSwitch', function(e){
var currentDiv = $("#" + e.currentTarget.id).bootstrapSwitch('state');
if( currentDiv == false){
var confirmed = confirm("Are you sure you wish to deactivate this user? They will no longer be able to access any forms.");
if(confirmed == true){
changeActivationStatus($(this).val());
} else {
$("#" + e.currentTarget.id).bootstrapSwitch('toggleState');
}
} else {
var confirmed = confirm("Are you sure you wish to activate this user? Deactivated users which were previously active will have the same permissions prior to their de-activation unless changed manually.");
if(confirmed == true){
changeActivationStatus($(this).val());
} else {
$("#" + e.currentTarget.id).bootstrapSwitch('toggleState');
}
}
});
function changeActivationStatus(userId){
$.post("{{ path('isactive') }}", {userId: userId})
.done(function(data){
console.log("Finished updating " + userId);
})
.fail(function(){
console.log("User could not be updated");
});
};
</script>