onclick ="return confirm('')"使用$('.class').click(function(){});

Fer*_*ndo 7 jquery confirm onclick

我有这个功能,用户点击后禁用输入:

$('.click-off').click(function () {
    var btn = this;
    setTimeout(function () { $(btn).attr('disabled', 'disabled'); }, 1);
    return true;
});
Run Code Online (Sandbox Code Playgroud)

我有这个输入与javascript确认:

<input type="submit" class="click-off" onclick="return confirm('Are you sure?');" value="Delete">
Run Code Online (Sandbox Code Playgroud)

即使我在确认框中单击"取消",也会调用$('.click-off').click()事件,并且该按钮将被禁用.

$('.click-off').click()被很多输入使用,因此确认不能在其中.

如何查看$('.click-off')中的确认返回.点击事件?甚至阻止$('.click-off').点击事件来调用?

Joe*_*ton 8

为什么你首先将这些逻辑分开?以下是解决问题的两种方法:

将逻辑组合到一个方法中:

$('.click-off').click(function () {
    // escape here if the confirm is false;
    if (!confirm('Are you sure?')) return false;
    var btn = this;
    setTimeout(function () { $(btn).attr('disabled', 'disabled'); }, 1);
    return true;
});
Run Code Online (Sandbox Code Playgroud)

使用全局变量(或最好是对象):

var clickOffConfirmed = false;

<input type="submit" class="click-off" onclick="clickOffConfirmed = confirm('Are you sure?');" value="Delete" />


$('.click-off').click(function () {
    // escape here if the confirm is false;
    if (!clickOffConfirmed) return false;
    var btn = this;
    setTimeout(function () { $(btn).attr('disabled', 'disabled'); }, 1);
    return true;
});
Run Code Online (Sandbox Code Playgroud)