简体   繁体   English

无法在 IE 中的输入字段上设置只读

[英]Can not set readonly on input field in IE

I have a simple input field:我有一个简单的输入字段:

<input id="myInput" class="someClass"></input>

and some JQuery code:和一些 JQuery 代码:

$(e.currentTarget).prop('readonly', true);

where e.currentTarget is that [object HTMLInputElement] as IE11 names it.其中e.currentTarget是 IE11 命名的[object HTMLInputElement]

I'm only trying to set this input field to be readonly.我只是想将此输入字段设置为只读。 In chrome that code works but in IE not.在 chrome 中该代码有效,但在 IE 中无效。 I tried already:我已经试过了:

.prop('readonly','readonly');
.prop('readonly', '');
.attr('readonly', true);

but none of them works in IE11 ( in chrome everyone of them works)但它们都不能在 IE11 中工作(在 chrome 中,每个人都可以工作)

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.好吧,这很奇怪:如果您获得焦点将该字段设为只读,则 IE11 似乎有点疯狂,其中一种疯狂的方法是让您在光标存在时继续修改该字段 — 使用一些击键,但不是其他击键。 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);
}

Adding this line before setting readOnly fixes it ( fiddle ):设置readOnly之前添加这一行修复它(小提琴):

$(e.currentTarget).blur();

Side note: You don't need jQuery to set the readOnly property, just:旁注:您不需要 jQuery 来设置readOnly属性,只需:

e.currentTarget.readOnly = true; // Note the capital O

'Read-only' input element doesn't work consistently in IE 8,9, 10 or 11. “只读”输入元素在 IE 8、9、10 或 11 中无法始终如一地工作。

In this case, we can use onkeydown="javascript: return false;"在这种情况下,我们可以使用onkeydown="javascript: return false;" in the input element.在输入元素中。

I have used Focusin() function in jquery side with Id.我在 jquery 端使用了 Focusin() 函数和 Id。 When I click on textbox then we remove readony attribute as below:当我单击文本框时,我们删除 readony 属性,如下所示:

HTML: HTML:

<input type="text" id="txtCustomerSearch" readonly class="customer-search" 
   placeholder="Search customer:" maxlength="100" autocomplete="off">

Jquery:查询:

$("#txtCustomerSearch").focusin(function () {
    $(this).removeAttr('readonly');

}); });

Note: it will working in IE11 and other browser.注意:它将在 IE11 和其他浏览器中工作。

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

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