我的项目中没有 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属性中传递事件
,实际上你只是使用该函数而不传递参数,尝试使用:
= button_to "Remove Item", item, method: :delete, onclick: 'remove_item(event)'
关于javascript - 如何使用纯javascript在rails中通过确认删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45603912/