使用 Rails,我可以做到这一点:
<%= link_to("Delete", product,
:method => :delete,
data: {confirm: 'Are you sure?.'}
id: 'discard_btn') %>
我知道,当收到点击时,标准的 JavaScript 确认对话框会询问“您确定吗?”如果单击“确定”,则发送请求的问题。
我想用一个具有相同行为的漂亮的 Bootbox 确认对话框来更改这个简单的对话框。我尝试这样做:
$('#employee_discard').click(function(e){
bootbox.confirm("Are you sure?", function(result) {
// How to send the delete request from this callback?
});
// Prevent the request to be sent. This works/
return false;
});
但我的问题是 Bootbox 使用回调来处理其事件,仅在回调中写入 return true 是行不通的。
如何从 JavaScript 发送删除请求?
最佳答案
这样的事情应该有效:
$.ajax({
type: "DELETE",
url: "/products/1"
})
并且在 Controller 中您需要添加js格式的响应
respond_to do |format|
format.js { render "something.js.erb" }
end
最后,something.js.erb 将被执行。例如可以刷新当前页面
location.reload();
关于jquery - Rails 从 javascript 删除链接调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31251709/