2015-02-05 67 views
3

我有一个简单的输入框:不能只读设置输入栏IE

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

和一些jQuery代码:

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

其中e.currentTarget[object HTMLInputElement]作为IE11的名字吧。

我只是试图设置这个输入字段是只读的。在代码工作,但IE中没有。 我想已经:

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

但他们都不在IE11作品(铬大家他们的作品)

+0

你试过.attr('readOnly',true); ?? – jrenk 2015-02-05 12:55:28

+0

@jrenk:如果'prop'不起作用,'attr'不会。 – 2015-02-05 12:56:59

+0

它工作正常。输入在IE中变为只读。 (我用$('input')'作为选择器)。 – MrUpsidown 2015-02-05 12:58:04

回答

4

好吧,这是离奇:如果您使字段只读它有一个焦点,IE11似乎有点疯狂,其中一个方法它去疯狂的是让你不断修改字段,而光标是有的  —与一些击键,但不是其他人。这里有一个例子: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); 
} 

添加此行设置readOnly修复它(fiddle)前:

$(e.currentTarget).blur(); 

旁注:你并不需要jQuery来设置readOnly属性,只是:

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

谢谢。但知道它会在铬中抛出关键的角度误差;)但这是另一个问题的主题。 – szpic 2015-02-05 13:19:41

+0

@szpic:趣味有趣的乐趣。在Chrome浏览器中,只有我的原始小提琴适合我... – 2015-02-05 13:27:49

1

“只读”输入元件不始终在IE 8,9,10或11。

在这种情况下,我们可以使用的onkeydown =工作“的javascript:返回false;”输入元素中的