开发者

Event triggering not completing

I have divs which can be toggled with an arrow anchor item. Once you click, the div shows it content or hides it (mantaining the header where the anchors and title are).

Next to the toggler, I have an icon which is supposed to delete the div but I wanted to make that if the dive is开发者_运维技巧 toggle_closed it fires the event of the toggler and open the div to see it before you decide if you are going to delete it or won't.

Here is my code:

    var toggler = parentDiv.find('a.toggle');
                    if (toggler.hasClass('toggle_closed'))
                        toggler.trigger('click',function(){

                        });
                    var option = parentDiv.find('input[name="tag"]').val();

                    var textConfirm = 'Are you sure you want to delete the "'+option+'"?';
                    if (confirm(textConfirm)){
do something;
}

The problem is that, when I click the delete button it fires the event, so the arrow changes but it doesn't show the div but ask for confirmation. If you cancel, the div is shown so I think I need to wait it to end the event.


.trigger() functions second parameter is 'extraParameters' so first try changing the line like this:

toggler.trigger('click');

That probably isn't your problem here. Do you have animation when toggling content divs? maybe the confirm is blocking the animation. Instead of triggering click, you can manually call toggle (or slideToggle) and use a callback when animation ends:

function showDelConfirm(targetDiv) {
    var option = targetDiv.find('input[name="tag"]').val();
    var textConfirm = 'Are you sure you want to delete the "' + option + '"?';
    if (confirm(textConfirm)) {
        do something;
    }
}

$('.deleteButton').click(function () {
    var parentDiv = $(this).parent();
    var toggler = parentDiv.find('a.toggle');
    if (toggler.hasClass('toggle_closed')) {
        $('.divToShow').toggle('fast', function () {
            showDelConfirm(parentDiv);
        });
    } else {
        showDelConfirm(parentDiv);
    }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜