Domanda

Ho link come questo.

<a href="delete.php?id=1" class="delete">Delete</a>

Se un utente fa clic su di esso. Una conferma dovrebbe popup e quindi solo se l'utente clicca sì, dovrebbe goto l'URL effettivo.

So che questo può impedire il comportamento di default

    function show_confirm()
    {
    var r=confirm("Are you sure you want to delete?");
    if (r==true)   {  **//what to do here?!!** }

    }    


    $('.delete').click(function(event) {
    event.preventDefault();
    show_confirm()
    });

Ma come faccio a continuare a quel link o inviare un messaggio ajax a quel link dopo aver confermato?

È stato utile?

Soluzione

si poteva fare tutto all'interno del clic:

$('.delete').click(function(event) {
    event.preventDefault();
    var r=confirm("Are you sure you want to delete?");
    if (r==true)   {  
       window.location = $(this).attr('href');
    }

});

o si potrebbe farlo passando l'elemento cliccato per la funzione:

function show_confirm(obj){
    var r=confirm("Are you sure you want to delete?");
    if (r==true)  
       window.location = obj.attr('href');
}    
$('.delete').click(function(event) {
    event.preventDefault();
    show_confirm($(this));

});

Altri suggerimenti

Mi c'è voluto un po 'per capirlo, così ho pensato che avrei posto la mia soluzione.

$('.delete').click(function(e){
    if(confirm('Are you sure?')){
        // The user pressed OK
        // Do nothing, the link will continue to be opened normally
    } else {
        // The user pressed Cancel, so prevent the link from opening
        e.preventDefault();
    }
}

I stava pensando confermano nel modo sbagliato. Conferma impedirà al sito di aprire automaticamente, e attendere l'input dell'utente. Quindi, fondamentalmente, è necessario spostare il preventDefault al resto.

Così si impedisce solo il link apertura se si clicca su Annulla. Ciò consente anche per il collegamento di funzionare come farebbe normalmente, ad esempio, se ha un target = "_ blank" istruzioni.

function show_confirm(url){
    var r=confirm("Are you sure you want to delete?");
    if (r==true){
        location.top.href = url;
    }
}    


$('.delete').click(function(event) {
    event.preventDefault();
    show_confirm($(this).attr('href'));
});

Se si desidera utilizzare Ajax è possibile sostituire con location.top.href = url; $.get(url);

function show_confirm(elem)
{
    var r=confirm("Are you sure you want to delete?");
    if (r==true) { 
        window.location.href = elem.href;
    }
}    

$('.delete').click(function(event) {
    event.preventDefault();
    show_confirm(this)
});

Questa è una forma breve:

$('.delete').click(function(){return confirm("Are you sure you want to delete?")});

Io lo uso su siti web per il download di conferma / link.

Per ottimizzare il codice nella funzione show_confirm, provare a utilizzare di seguito:

function show_confirm(obj){
   if(confirm("Are you sure you want to delete?")) window.location = obj.attr('href');
}

si potrebbe fare

function show_confirm()
    {
    if(confirm("Are you sure you want to delete?")){
        //make ajax call
     }else{

          //no ajax call
     }

    }    
 $('.delete').click(function() {

   if (confirm('Are you sure?')) {
     $.post($(this).attr('href'), function() {
       // Delete is OK, update the table or whatever has to be done after a succesfull delete
      ....
     }
   }

   return false;

}

Se si desidera utilizzare alert / confermano, questo è il modo migliore (io preferisco usare Bootstrap Conferma o bootbox ):

$('.confirm-delete').click( function( event ) {
    if ( !confirm('Are you sure?') ) event.preventDefault();
});
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top