問題1:當我點擊鏈接時,sweet alert modal 會彈出一瞬間,然后消失,然后重定向到路由。不知道為什么模態只在它應該保持直到選擇選項時才閃爍。我需要用戶確認是或否來洗掉帳戶。如果不是,則取消,如果是,則重定向到路由。

問題 2:通過查看此檔案,我還了解到,為了在 Sweet Alert 中使用 DELETE 方法,我必須使用 ajax 請求。不確定要在 url 中輸入什么,因為我正在使用帶有 DELETE 方法的 rails 路由,以便讓 ajax 重定向到該路由以洗掉帳戶。
任何幫助表示贊賞。這兩天一直在做這件事。
編輯:Rails 版本 4.2.4,Sweet Alert 版本 1
<%= link_to 'Delete Account',registration_path(current_user), method: :delete, data: { behavior: 'delete' } %>
$("[data-behavior='delete']").on('click', function (e) {
e.preventDefault();
swal ({
title: 'Are you sure?',
text: 'You will not be able to recover your account!',
icon: 'warning',
buttons: [ 'Yes', 'No']
}).then(isNo => {
if(isNo) return;
$.ajax({
url: $(this).attr('href'),
dataType: "JSON",
method: "DELETE",
success: ()=> {
swal('Deleted!', 'Your account has been deleted.', 'success');
}
})
});
});
uj5u.com熱心網友回復:
比我想象的要簡單。使用 form_tag 并在 jQuery 中使用$(this).parents('form');如果您嘗試通過 id 抓取表單來提交,它將無法正常作業。不需要使用 AJAX。
HTML
<%= form_tag(registration_path(current_user), { method: :delete, id: "deleteForm" }) do %>
<button id="delete" type="submit"><i class="fa fa-trash"></i>Delete Account</button>
<% end %>
jQuery
$('#delete').on('click', function (e) {
e.preventDefault();
var form = $(this).parents('form');
swal ({
title: 'Are you sure?',
text: 'You will not be able to recover your account!',
icon: 'warning',
closeModal: false,
allowOutsideClick: false,
closeOnConfirm: false,
closeOnCancel: false,
buttons: [ 'No', 'Yes']
}).then((willDelete) => {
if (willDelete) {
swal("Your account has been deleted!", {
icon: "success",
});
form.submit();
} else {
swal("Your account is safe.");
}
});
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/477401.html
標籤:javascript 轨道上的红宝石 ruby-on-rails-4 甜心提醒
