2011-06-17 15 views
16

Ho una chiamata ajax associata a un collegamento tramite jQuery e la voglio intercettata da una finestra di conferma. Ma la chiamata ajax si attiva indipendentemente dall'opzione selezionata (anche se l'utente chiude la finestra di dialogo).Intercettare una chiamata jQuery.ajax() con confirm()

C'è un modo per ottenere che la conferma funzioni come avviene nei contesti sincroni?

HTML:

<a href="#" class="removeItem delete">remove</a> 

jQuery:

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


$('.removeItem').click(function (event) { 
    event.preventDefault(); 

    $.ajax({ 
     url: 'myUrl', 
     type: "POST", 
     data: { 
      // data stuff here 
     }, 
     success: function() { 
      // does some stuff here... 
     } 
    }); 
}); 

risposta

66
$('.removeItem').click(function (event) { 
    if (confirm('Are you sure you want to delete this?')) { 
     $.ajax({ 
      url: 'myUrl', 
      type: "POST", 
      data: { 
       // data stuff here 
      }, 
      success: function() { 
       // does some stuff here... 
      } 
     }); 
    } 
}); 
Problemi correlati