在同一链接中运行2个onclick函数

时间:2013-07-23 09:22:51

标签: jquery function

这是我的问题。我想在点击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>

2 个答案:

答案 0 :(得分: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;
});

答案 1 :(得分: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
}