簡體   English   中英

如何在Rails中使用純JavaScript進行刪除和確認?

[英]How to use plain javascript to delete with confirm in rails?

我的項目中沒有jquery_ujs,因此,當我單擊刪除按鈕時,我試圖彈出一個窗口。

但是,即使我按“取消”或“確定”,該項目也將被刪除。

這是代碼:

= button_to "Remove Item", item, method: :delete, onclick: 'remove_item()'

function remove_item(event){
    if (!confirm('Are you sure, you want to delete the item')) {
       event.preventDefault();
    }
  }

你能幫我解決這個問題嗎? 謝謝。

您必須在onclick屬性中傳遞event ,實際上您只是在使用函數而不傳遞參數,請嘗試:

= button_to "Remove Item", item, method: :delete, onclick: 'remove_item(event)'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM