2

I am new to javascript and web development, now I am experimenting in codeigniter and I want to setup a javascript confirmation box on a delete link. Now I got it to work reasonably well using this:

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

        return false;
    }
</script>

and this:

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>';

The problem I have is that the first time one clicks the link there is no popup, but every click after that works as it should. I also feel that maybe I should use document.getElementById and window.onload to get it to work properly. Any help would be greatly appreciated.

I eventually used a jquery solution looking like this:

$(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;
        }
    });
})

});

I use the commercial msgUI for the msgAlert box.

4

1 に答える 1

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

于 2012-09-18T08:21:26.800 に答える