简体   繁体   English

禁用按钮仍然使用“.click()”触发

[英]Disabled button still fires using “.click()”

It seems disabled button "onclick" function is still fired when triggering it programmaticaly, eg: 似乎禁用按钮 “onclick”功能在触发程序时仍然被触发,例如:

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

see http://jsfiddle.net/WzEvs/363/ http://jsfiddle.net/WzEvs/363/

In my situation, keyboards shortcuts are bound to functions triggering the ".click()" on buttons. 在我的情况下,键盘快捷键绑定到触发按钮上的“.click()”的功能。 I'll find it very annoying to have to disable the shorcuts or check if the button is disabled myself. 我发现必须禁用shorcuts或检查按钮是否已被禁用非常烦人。 I'd prefer a general solution fixing this problem. 我更喜欢解决这个问题的一般解决方案。

  • But why? 但为什么? This behavior doesn't seem fair to me. 这种行为对我来说似乎不公平。
  • Any workaround? 任何解决方法?

The attribute only disables user interaction, the button is still usable programmatically. 该属性仅禁用用户交互,该按钮仍可通过编程方式使用。

So yeah, you gotta check 所以是的,你得检查一下

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

Alternatively don't use inline handlers. 或者,不要使用内联处理程序。

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

For future use...the OP code works because jQuery will still call it's own handlers even if the DOM element is disabled. 为了将来使用... OP代码可以工作,因为即使DOM元素被禁用,jQuery仍然会调用它自己的处理程序。 If one were to use vanilla javascript, the disabled attribute would be honored. 如果要使用vanilla javascript,则会禁用disabled属性。

const element = document.getElementById('saveButton');
element.click() //this would not work

You can programmatically trigger click on a disabled button. 您可以通过编程方式触发单击禁用按钮。

There are ways to find if the event is a click on button by user or it has been trigger programmatically. 有一些方法可以查找事件是否是用户单击按钮,或者是否已通过编程方式触发。 http://jsfiddle.net/WzEvs/373/ http://jsfiddle.net/WzEvs/373/

$(function () {
    $("#saveButton").on('click', function (e) {
        if (!e.isTrigger) {
            var count = parseInt($('#counter').html());
            $('#counter').html(++count);
        }
    });
    $("#bypassButton").on('click', function (e) {
        $("#saveButton").click();
    });
});

e.isTrigger is true if you call the click() programmatically. 如果以编程方式调用click()e.isTrigger为true。 Basically you are triggering the click event manually in code. 基本上你是在代码中手动触发click事件。

You can trigger click still although made it disable .As Spokey said it just shows the user-interaction(the usability still persists that can be turned on programmatically) . 你可以触发click静止,虽然它被disable 。正如Spokey说它只是显示用户交互(可用性仍然存在,可以通过编程方式打开)。 off or unbind the click will solve this issue. offunbind点击将解决此问题。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM