简体   繁体   English

多个确认框Rails link_to

[英]Multiple confirm boxes Rails link_to

Let's say that I have a link on my website that destroys the world when clicked: 假设我的网站上有一个链接,当您单击该链接时,它会破坏世界:

<%= link_to "Destroy the world", @world, :confirm => 'Are you sure?', :method => :delete %>

Obviously, one confirm box isn't enough here. 显然,一个确认框还不够。 Someone might destroy the world by accident. 有人可能会无意间破坏世界。

What I want to figure out is how to display multiple confirm boxes, one after the other. 我想弄清楚的是如何一个接一个地显示多个确认框。 So for example, instead of only being presented with a box that says "Are you sure?", my users will be presented with "Are you sure?" 因此,例如,不仅仅向其显示“您确定吗?”框,还向我的用户显示“您确定吗?” -> "Are you REALLY sure?" ->“您确定吗?” -> "Are you sure that you're sure?", until I finally decide that they're sure enough, and let them destroy the world. ->“您确定要确定吗?”,直到我最终确定他们足够确定并让他们摧毁世界。

What's the best way to accomplish this? 做到这一点的最佳方法是什么?

The Rails code: Rails代码:

<%= link_to "Destroy the world", @world, :confirm => 'Are you sure?', :method => :delete, :id => "destroy-the-world-id" %>

The Javascript: Javascript:

function confirmWorldDestroy() {
    return confirm('Are sure?') &&
           confirm('Are you REALLY sure?') &&
           confirm('Are you REALLY sure you\'re sure?');
}

$(function() {
    $('#destroy-the-world-id').click(function(e) {
        return confirmWorldDestroy();
    });
});

如果您退回到内联javascript,则非常简单。

<%= link_to "Destroy the world", @world, :method => :delete, :onclick => "return confirm('Are you sure?') && confirm('Are you REALLY sure?') && confirm('Are you sure that you\'re sure?')" %>

Maybe you should add a function that you'd call upon submission (:on submit) 也许您应该添加一个在提交时调用的函数(:提交时)

It would display all confirmation prompts and return true if all confirmation are received or false otherwise. 它会显示所有确认提示,如果收到所有确认,则返回true,否则返回false。

If you get a false return, I think the form submission will be cancelled. 如果您得到错误的回报,我认为表格提交将被取消。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM