如果在jquery中已经禁用了按键事件,如何启用按键事件

时间:2022-06-02 20:37:20

Below is the code where I am disabling ENTER button.

下面是我禁用ENTER按钮的代码。

$(document).ready(function () {
    $(window).keydown(function (event) {
        if ((event.keyCode == 13) && ($('#btnsubmit').disabled)) {
            event.preventDefault();
            return false;
        }
    });

In some cases I want to enable it and want the ENTER key to work as expected. Could someone help me out in fixing this?

在某些情况下,我想启用它,并希望ENTER键按预期工作。有人可以帮我解决这个问题吗?

1 个解决方案

#1


use the flag approach:

使用标志方法:

var enterDisabled = true;

$(document).ready(function() {

  $(window).keydown(function(event) {
    // check if enterDisabled flag is on and disable enter
    if (enterDisabled && (event.keyCode == 13) && ($('#btnsubmit').prop('disabled'))) {
      alert("enter is disabled");
      event.preventDefault();
      return false;
    }
  });
  $("#enterToggler").click(function() {
    enterDisabled = !enterDisabled;
    $("#status").text(enterDisabled);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="submit" id="btnsubmit" value="submit" disabled="disabled" />
<button id="enterToggler">Toggle Enter</button>
<br/>Enter Disabled: <span id="status">true</span>

simply set the flat to false when you need the enter to work...

当你需要输入工作时,只需将公寓设置为假...

#1


use the flag approach:

使用标志方法:

var enterDisabled = true;

$(document).ready(function() {

  $(window).keydown(function(event) {
    // check if enterDisabled flag is on and disable enter
    if (enterDisabled && (event.keyCode == 13) && ($('#btnsubmit').prop('disabled'))) {
      alert("enter is disabled");
      event.preventDefault();
      return false;
    }
  });
  $("#enterToggler").click(function() {
    enterDisabled = !enterDisabled;
    $("#status").text(enterDisabled);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="submit" id="btnsubmit" value="submit" disabled="disabled" />
<button id="enterToggler">Toggle Enter</button>
<br/>Enter Disabled: <span id="status">true</span>

simply set the flat to false when you need the enter to work...

当你需要输入工作时,只需将公寓设置为假...