[Answer]-Confirmation on multiple buttons in django template

1👍

confirmed is always false because javascript is asynchrone.

You need to use some closure. Something like this should do the trick:

    $('input:submit').click(function() {
        // get form associated with the button
        var form = $(this).parents('form')
        $( "#dialog-confirm" ).dialog({
            resizable: false,
            height:140,
            modal: true,
            buttons: {
                "Yes": function() {
                    $( this ).dialog( "close" );
                    // maybe set an hidden field to keep button value if needed
                    form.submit()
                },

                "No": function() {
                    $( this ).dialog( "close" );
                }
            }
        });
        return false;
    }
👤gawel

0👍

Another problem is that I also have other buttons designed this way (form, submit) on my site.
But I’ve got it work with this piece of code:

<script>
     $('.popup_button').click(function() {
         // get form associated with the button
         var form = $(this)

         $("#dialog-confirm").dialog({
             resizable: false,
             height:200,
             modal: true,
             buttons: {
                 "Yes": function() {
                     $( this ).dialog( "close" );
                     form.submit()
                 },

                 "No": function() {
                    $( this ).dialog( "close" );
                 }
             }
         });
         return false;
     });
</script>

<div id="dialog-confirm" title="Confirmation" style='display: none'>
    <p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>
        Are you sure?</p>
</div>

So confirmation popup appears only for those buttons, which have class ‘popup_button’.
The only thing I’m missing is how t get custom confirmation message for each button.

When I substitute

$("#dialog-confirm").dialog

with

$(".new_button_class").dialog

and then add class=’new_button_class’ and ‘title’ attributes to my button forms, strange things happen. What is more, I don’t now how to set dialog message this way.

adding

$(".new_button_class", form).dialog

instead causes popups not to appear at all.

How to fix it?

Leave a comment