2013-07-23 42 views
0

這是我的問題。我想在點擊一個href鏈接時運行一個刪除功能,但我也想要一個確認彈出窗口。目前,刪除腳本正在運行,如果我點擊確定或取消,顯然我只希望它運行,如果我點擊確定。下面是代碼:在同一鏈接中運行2個onclick函數

<a class=\"deletelink delete$postid\" href=\"#\" data-post=\"$postid\" data-type=\"$posttype\" data-file=\"$postmedia\" onclick=\"return deletepost();\">Delete</a> 

<script type="text/javascript"> 
function deletepost(){ 
var deletequestion = confirm("You are about to delete one of your posts"); 
if(deletequestion){ 
return true; 
}else{ 
return false; 
} 
} 
</script> 

<script type="text/javascript"> 
    $('.deletelink').on('click', function() 
    { 
    var post = $(this).attr("data-post"); 
    var file = $(this).attr("data-file"); 
    var type = $(this).attr("data-type"); 
    jQuery.post("php/delete.php", { 
    post:post, 
    file:file, 
    type:type 
    }, function(data, textStatus){ 
    if(data == 1){ 
    $('.delete' + post).html("Deleted").addClass("disableClick"); 
    }else{ 
    $('.delete' + post).html("Error"); 
    } 
    }); 
    return false; 
    }); 
</script> 

回答

2

你需要俱樂部在一起

<a class=\"deletelink delete$postid\" href=\"#\" data-post=\"$postid\" data-type=\"$posttype\" data-file=\"$postmedia\">Delete</a> 

而且

$('.deletelink').on('click', function() { 
    var deletequestion = confirm("You are about to delete one of your posts"); 
    if(!deletequestion){ 
     return; 
    } 
    var post = $(this).attr("data-post"); 
    var file = $(this).attr("data-file"); 
    var type = $(this).attr("data-type"); 
    jQuery.post("php/delete.php", { 
     post:post, 
     file:file, 
     type:type 
    }, function(data, textStatus){ 
     if(data == 1){ 
      $('.delete' + post).html("Deleted").addClass("disableClick"); 
     }else{ 
      $('.delete' + post).html("Error"); 
     } 
    }); 
    return false; 
}); 
+0

我不能相信的解決方案是如此簡單。非常感謝 – user2516546

0

'@Arun P約翰尼拉' 是完全正確的。我也是會建議是這樣的:

<a ... confirmation='deleteCheck' ../> 

+

function deleteCheck() { 
     var deletequestion = confirm("You are about to delete one of your posts"); 
     // and so on ... 
    } 

$('.deletelink').on('click', function() { 
    var link = $(this); 
    var confirmation = link.attr('confirmation'); 
    if (confirmation != undefined && !window[confirmation]()) { 
     return; 
    } 

    ... // continue 
}