禁用按钮仍然使用“.click()”触发 [英] Disabled button still fires using ".click()"

查看:551
本文介绍了禁用按钮仍然使用“.click()”触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎禁用按钮onclick函数在触发程序时仍会触发,例如:

It seems disabled button "onclick" function is still fired when triggering it programmaticaly, eg:

<div>
   <input type="button" onclick="save()" id="saveButton"    value="save"  disabled="disabled" />
   <input type="button" onclick="byPassDisabled()" value="bypass disabled button"/>
<div id="counter">0</div>

function save(){
    var count = parseInt($('#counter').html());
    $('#counter').html(++count);
}
function byPassDisabled(){
     $('#saveButton').click();   
}

参见 http://jsfiddle.net/WzEvs/363/

在我的情况下,键盘快捷键绑定到功能触发按钮上的.click()。我发现必须禁用shorcuts或检查按钮是否已被禁用非常烦人。我更喜欢解决此问题的一般解决方案。

In my situation, keyboards shortcuts are bound to functions triggering the ".click()" on buttons. I'll find it very annoying to have to disable the shorcuts or check if the button is disabled myself. I'd prefer a general solution fixing this problem.


  • 但为什么?这种行为对我来说似乎不公平。

  • 任何解决方法?

推荐答案

该属性仅禁用用户交互,该按钮仍可通过编程方式使用。

The attribute only disables user interaction, the button is still usable programmatically.

所以是的,你必须检查

function byPassDisabled(){
    $('#saveButton:enabled').click();   
}

或者不要使用内联处理程序。

Alternatively don't use inline handlers.

$(document).on('click', '#saveButton:enabled', function(){
    // ...
});

这篇关于禁用按钮仍然使用“.click()”触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆