JavaScript的确认弹出(Javascript confirmation popup)

2019-09-22 13:43发布

我是新来的JavaScript和Web开发,现在我笨我尝试,我想建立一个删除链接,一个JavaScript确认框。 现在,我得到它的工作相当不错使用此:

<script type="text/javascript">
    function con(message) {
        var answer = confirm(message);
        if (answer)
        {
            return true;
        }

        return false;
    }
</script>

和这个:

echo '<a href="/groups/deletegroup/'.$group->id.'" OnClick="return con(\'Are you sure you want to delete the group?\');" class="btn small light-grey block">Delete</a>';

我的问题是,第一次一个点击该链接不存在弹出,但之后每次点击的作品,因为它应该。 我也觉得,也许我应该使用的document.getElementById并在window.onload得到它才能正常工作。 任何帮助将不胜感激。

我最终用一个jQuery的解决方案看起来像这样:

$(document).ready(function (){
$('.delete').click(function (e) {
    e.preventDefault();
    var href = $(this).attr('href');
    $.msgAlert({
        title: 'Delete'
        ,text: 'Do you really want to delete this group?'
        ,type: 'error'
        ,callback: function () {
                location.href = href;
        }
    });
})

});

我用的是商业msgUI为msgAlert箱。

Answer 1:

尝试这个...

HTML:

<a href="#" id="btn-delete">Delete</a>

JS:

//window.onload = function() {
    var btnDelete = document.getElementById('btn-delete');
    btnDelete.onclick = function() {
        var message = 'Are you sure you want to delete the group?';
        if (confirm(message)) {
            // delete something...
        }
    }
//};

http://jsfiddle.net/PEHx9/



文章来源: Javascript confirmation popup