javascript - IE11 不想关注 `disabled` 元素

标签 javascript jquery html internet-explorer-11

我试图强制文本只能在两个文本字段之一中输入。

当一个字段失去焦点时,我检查它的值,如果它不为空,我禁用另一个文本字段。

这是一个例子:

HTML:

<div class="container">
  <label for="textOne">textOne</label>
  <input type="text" id="textOne"/>
</div>
<div class="conatiner">
  <label for="textTwo">textTwo</label>
  <input type="text" id="textTwo"/>
</div>

jQuery:

$("#textOne").on('focusout', function() {
  console.log("focusout::\t"+this.id);
  if( $("#textOne").val() == "") {
    $("#textTwo").prop('disabled', false);
  } else {
    $("#textTwo").prop('disabled', true);
  }
});

$("#textTwo").on('focusout', function() {
  console.log("focusout::\t"+this.id);
  if( $("#textTwo").val() == "") {
    $("#textOne").prop('disabled', false);
  } else {
    $("#textOne").prop('disabled', true);
  }
});

这在 Chrome 和 Firefox 中工作得很好,但似乎 IE11 不支持 focus disabled 元素。

我找到的唯一解决方案是 this question , 即使用 readonly 属性,而不是 disabled 属性。这不是我的应用程序理想的解决方案。

有没有办法在 IE11 中实现这一点,同时仍然使用 disabled 属性?

IE11 不支持 focus disabled 属性的原因是什么?

提前感谢您的任何建议和回答。

编辑:这是一个关于 jsFiddle 的示例,当它在 IE11 上运行时将重现帖子中解释的问题 https://jsfiddle.net/ftqbop7a/2/

最佳答案

就这么简单,而不是 focusout 使用 input 事件:

$("#textOne").on('input', function() {
    if( $.trim($("this").val()) == "") {
    $("#textTwo").prop('disabled', false);
  } else {
    $("#textTwo").prop('disabled', true);
  }
});

$("#textTwo").on('input', function() {
    if( $("#textTwo").val() == "") {
    $("#textOne").prop('disabled', false);
  } else {
    $("#textOne").prop('disabled', true);
  }
});

jsFiddle

解释一下:在 focusout 上,IE 无法获取最后操作的输入元素的引用,而 (on)input 事件会,因为它会立即发生。


想要简化您的代码并让您的老板赞叹不已?
jsFiddle

var $inp = $("#textOne, #textTwo");
$inp.on("input", function() {
   $inp.not(this).prop("disabled", $.trim(this.value));
});

关于javascript - IE11 不想关注 `disabled` 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36927325/

相关文章:

javascript - 使用 d3.create 创建和附加分离的元素

javascript - jQuery 用户界面 : How can I have more than one "selectable" element if the element has to have id ="selectable"?

javascript - 使用jquery突出显示具有最小值的html列

javascript - 在其他表格中按下按钮时如何在html表格列中执行动态计算

javascript - run 方法中的 Angular $q 服务实现

javascript - Extjs从一个 View 访问另一个 View

javascript - 使用 setInterval 在 jquery 中闪烁带有背景颜色的 div

javascript - 我还应该将我的数据保存在 Javascript 数据结构中吗?或者只是在 DOM 中?

javascript - HTML 元素的唯一标识符

javascript - 销售人员 : JavaScript Remoting "Uncaught ReferenceError: Visualforce is not defined"