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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14059030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:14:11  来源:igfitidea点击:

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

jqueryonclickconfirm

提问by Fernando

I have this function that disables the input after user clicks:

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

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

And I have this input with a javascript confirmation:

我有一个带有 javascript 确认的输入:

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

Even if I click "cancel" in the confirmation box, the $('.click-off').click() event is called, and the button becomes disabled.

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

The $('.click-off').click() is used by a lot of inputs, so the confirmation can't be inside of it.

$('.click-off').click() 被很多输入使用,所以确认不能在里面。

How can I check the confirm return in the $('.click-off').click event? Or even prevent the $('.click-off').click event to be called?

如何检查 $('.click-off').click 事件中的确认返回?或者甚至阻止 $('.click-off').click 事件被调用?

回答by Joel Etherton

Why would you have these pieces of logic be separate in the first place? Here are 2 methods to get around the problem:

为什么你首先要把这些逻辑部分分开?这里有两种方法可以解决这个问题:

Combine the logic into a single method:

将逻辑组合成一个方法:

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

Use a global variable (or object preferably):

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

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

回答by povilasp

You should avoid hooking actions both with jquery and onclick, primarily because soon you will be totally lost in your code.

您应该避免同时使用 jquery 和 onclick 挂钩操作,主要是因为很快您将完全迷失在您的代码中。

So for example you can do this:

例如,您可以这样做:

$('.click-off').click(function () {

   var r=confirm("Are you sure?")
   if (r==true)
   {
       var btn = this;
       setTimeout(function () { $(btn).attr('disabled', 'disabled'); }, 1);
       return true;
   }
   else
   {
       //nothing to do here
   }

});

And remove the onclick event. So you will have everything in one place.

并删除 onclick 事件。因此,您将在一处拥有所有内容。

In your example you hook twice to the click event, and they both get fired despite the results of each other.

在您的示例中,您将两次连接到 click 事件,尽管彼此的结果不同,但它们都会被触发。

回答by Pedro Estrada

Try capturing the confirm box then disabling the button.

尝试捕获确认框,然后禁用该按钮。

$('.click-off').click(function () {
  var r=confirm("Press a button");
  if (r==true)
  {
    $(this).attr('disabled', 'disabled');
  }
  else
  {

  }
});

also remove the onclick

也删除 onclick