0

这是我的问题。我想在单击 href 链接时运行删除功能,但我也希望有一个确认弹出窗口。目前,如果我单击“确定”或“取消”,删除脚本正在运行,所以显然我只希望它在单击“确定”时运行。这是代码:

<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>
4

2 回答 2

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;
});
于 2013-07-23T09:24:52.167 回答
0

'@Arun P Johny' 非常正确。我也会建议类似的东西:

<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
}
于 2013-07-23T09:34:15.990 回答