如何使用 jQuery (IE) 删除禁用属性 [英] How to remove disabled attribute with jQuery (IE)
问题描述
我必须使用 disabled 属性来停用我不希望用户编辑的所有输入.
I'm in a situation where I have to use the disabled attribute to deactivate all inputs I don't want the user to edit.
<input disabled="<%= disableInputs%>" type="text"></input>
渲染
<input disabled="False" type="text"></input>
或
<input disabled="True" type="text"></input>
这在 Chrome 和 FF 上运行良好,但在 IE 上则不行.
This works fine on Chrome and FF, but on IE it does not.
现在我正在尝试使用 javascript 删除这些属性 (where disabled="False"),但我没有得到预期的结果.再说一次,javascript 可以在 Chrome 和 FF 上运行,但不适用于 IE(使用 8).
Now I'm trying to remove those attributes (where disabled="False") with javascript, but I don't get the expected results. Againt, it the javascript works gor Chrome and FF, but not for IE (using 8).
最后应该是这样的:
<input type="text"></input>
或
<input disabled="True" type="text"></input>
我的 javascript 看起来像(我几乎尝试过任何组合):
my javascript looks like (i've tried almost any combination):
$('document').ready(function () {
$("input[disabled='false']").each(function(){$(this).attr('disabled', false);});
$("select[disabled='false']").each(function(){$(this).attr('disabled', false);});
$("input[disabled='False']").each(function(){$(this).attr('disabled', false);});
$("select[disabled='False']").each(function(){$(this).attr('disabled', false);});
$("input[readonly='false']").each(function(){$(this).attr('readonly', false);});
$("select[readonly='false']").each(function(){$(this).attr('readonly', false);});
$("input[readonly='False']").each(function(){$(this).attr('readonly', false);});
$("select[readonly='False']").each(function(){$(this).attr('readonly', false);});
$("input[disabled='false']").each(function(){$(this).removeAttr('disabled');});
$("select[disabled='false']").each(function(){$(this).removeAttr('disabled');});
$("input[disabled='False']").each(function(){$(this).removeAttr('disabled');});
$("select[disabled='False']").each(function(){$(this).removeAttr('disabled');});
$("input[readonly='false']").each(function(){$(this).removeAttr('readonly');});
$("select[readonly='false']").each(function(){$(this).removeAttr('readonly');});
$("input[readonly='False']").each(function(){$(this).removeAttr('readonly');});
$("select[readonly='False']").each(function(){$(this).removeAttr('readonly');});
});
一个活生生的例子:http://jsfiddle.net/2LNa6/ 更新
推荐答案
您应该使用 jQuery 中的 .prop 方法,因为它会为您绕过这些烦人的 IE 错误进行完整性检查.我可以看到您正在使用 jQuery 1.6,所以这应该可以工作:
You should use the .prop method in jQuery as it does sanity checking for you bypassing these annoying IE errors. I can see you're using jQuery 1.6, so this should work:
$('document').ready(function () {
//get each input that is disabled
$('input').each(function(i, el){
//see if it should be disabled (true|false)
var disabled = $(el).data('disabled');
$(el).prop('disabled', disabled);
});
});
这是更新后的jsFiddle供您查看.
这篇关于如何使用 jQuery (IE) 删除禁用属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!