jQuery-我们有类似“OnBeforeSomeEvent”的东西吗?

时间:2013-04-27 09:38:33

标签: jquery events

我们在jQuery中有任何类似“onBeforeSomeEvent”的事件吗?

对于Ex,

<input type='button' class='confirmdelete' onclick='delete();' value='DELETE' />

现在我想显示一个自定义对话框进行确认。 (我不想在JavaScript中使用默认确认方法)。

所以我需要类似的东西,

$('.confirmdelete').onBeforeClick(function() {
     if(true) //I show the custom dialog here and check some conditions. 
        return true;
     return false;
});

如果我从onbeforeclick事件中获得'true',则onclick事件应该触发。

抱歉,如果我感到困惑。可能就像手风琴中的beforeActivate一样。

5 个答案:

答案 0 :(得分:1)

为什么不直接包装delete()?

将你的验证放在onclick()事件上,如果是,则从那里删除它。

答案 1 :(得分:0)

也许,.mousedown()可能对您有帮助。

答案 2 :(得分:0)

将唯一id添加到删除按钮。

尝试使用jQuery和.click()jQuery UI dialog功能。

$('.confirmdelete').click(function () {
    var cur_id = this.id;
    var buttons = '<div id="dialog' + cur_id + '" title="Confirm" align="center"><input type="button" value="Yes" id="yes' + cur_id + '" /><input type="button" value="No" id="no' + cur_id + '" /></div>'

    $(this).after(buttons);
    $("#dialog" + cur_id).dialog();

    $("#yes" + cur_id).click(function(){
        alert('true - ' + cur_id);
        //Write your delete code here
        $(this).parents(".ui-dialog-content").dialog('close');
        $("#dialog" + cur_id).remove();
    });

    $("#no" + cur_id).click(function(){
        $(this).parents(".ui-dialog-content").dialog('close');
        $("#dialog" + cur_id).remove();
    });
});

将警报替换为注释部分中的删除代码。以下是一个示例: JSFIDDLE

答案 3 :(得分:0)

此链接可以帮助您jQuery delete confirmation box

还会先触发确认删除,然后执行该事件。

答案 4 :(得分:0)

你可以这样做:

<强> HTML

<input type='button' class='confirmdelete' value='DELETE' />

JS

$('.confirmdelete').click(function () {
    var del = confirm('Are you sure?');
    if (del) {
        delete();  // Just call your "delete()" method here..
        return true;
    } else {
        return false;
    }
});
相关问题