2011-06-17 58 views
16

我有一個通過jQuery綁定到鏈接的ajax調用,我希望它通過確認對話框截獲。但是無論選擇哪個選項,即使用戶剛剛關閉對話框,ajax調用也會被觸發。用confirm()調用jQuery.ajax()調用

有沒有辦法讓確認工作,因爲它在同步上下文?

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... 
     } 
    }); 
}); 

回答

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... 
      } 
     }); 
    } 
}); 
相關問題