无法在IE中的输入字段上设置只读 [英] Can not set readonly on input field in IE
问题描述
我有一个简单的输入字段:
I have a simple input field:
<input id="myInput" class="someClass"></input>
和一些JQuery代码:
and some JQuery code:
$(e.currentTarget).prop('readonly', true);
其中 e.currentTarget
是 [object HTMLInputElement]
因为IE11命名它。
where e.currentTarget
is that [object HTMLInputElement]
as IE11 names it.
我只是试图将此输入字段设置为只读。在Chrome中,代码可以工作,但在IE中却没有。
我已经尝试过了:
I'm only trying to set this input field to be readonly. In chrome that code works but in IE not. I tried already:
.prop('readonly','readonly');
.prop('readonly', '');
.attr('readonly', true);
但它们都不适用于IE11(在Chrome中,每个人都可以使用)
but none of them works in IE11 ( in chrome everyone of them works)
推荐答案
好的,这是奇怪的:如果你将字段设为只读而它有焦点,IE11似乎有点疯狂,其中一个方法是让你在光标存在的同时不断修改字段 —使用一些键击,但不是其他键击。以下是一个例子:小提琴
Okay, this is bizarre: If you make the field read-only while it has focus, IE11 seems to go a bit bonkers, and one of the ways it goes bonkers is to let you keep modifying the field while the cursor is there — with some keystrokes, but not others. Here's an example: Fiddle
$("#myInput").one("click", function(e) {
$(e.currentTarget).prop('readonly', true);
display("e.currentTarget.readOnly: " + e.currentTarget.readOnly);
});
$("#myInput").on("keydown", function(e) {
display("e.currentTarget.readOnly: " + e.currentTarget.readOnly);
});
function display(msg) {
$("<p>").html(String(msg)).appendTo(document.body);
}
在设置<$ c $之前添加此行 c> readOnly 修复它(小提琴):
$(e.currentTarget).blur();
附注:你不需要jQuery来只需设置 readOnly
属性:
e.currentTarget.readOnly = true; // Note the capital O
这篇关于无法在IE中的输入字段上设置只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!