简体   繁体   中英

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?

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

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.

If you get a false return, I think the form submission will be cancelled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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