jQuery 道具(“禁用”,真);AND attr('disabled', 'disabled') 在 chrome 和 firefox 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10414703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
prop("disabled", true); AND attr('disabled', 'disabled') is not working in chrome and firefox
提问by john Gu
i have the following script inside my asp.net mvc view:-
我的 asp.net mvc 视图中有以下脚本:-
function disableform(id) {
$('#' + id).prop("disabled", true);
}
But the above function will only disable the elements using internet explorer ,, but will fail to work on chrome or firefox, i also tried to write attr('disabled', 'disabled')
instead of .prop("disabled", true);
, but it did not solve the problem.
但是上述功能只会禁用使用 Internet Explorer 的元素,但在 chrome 或 firefox 上无法运行,我也尝试编写attr('disabled', 'disabled')
而不是.prop("disabled", true);
,但它没有解决问题。
my Jquery version is 1.7.1
我的 Jquery 版本是 1.7.1
So what might be the problem?
那么可能是什么问题呢?
BR
BR
回答by ilyes kooli
回答by thinklarge
I run ASP.NET and had this same issue.
我运行 ASP.NET 并遇到了同样的问题。
I ditched Jquery and went to pure Javascript and it worked great.
我放弃了 Jquery,转而使用纯 Javascript,效果很好。
var element = document.getElementById('MyID');
element.setAttribute('disabled', 'disabled');
Edit: For this to work properly you have to use element.removeAttribute('disabled'); when enabling the element. Otherwise it remains disabled.
编辑:为了使其正常工作,您必须使用 element.removeAttribute('disabled'); 启用元素时。否则它保持禁用状态。
回答by Sebastian Castaldi
I could not get it to work in chrome removing the property, so I just added the disabled class, it is not a true disabled but it works on IE and Chrome
我无法让它在 chrome 中删除该属性,所以我只是添加了 disabled 类,它不是真正的禁用,但它适用于 IE 和 Chrome
$('#search').on('click', function (event) {
$('#search').text("Searching");
$('#search').addClass("disabled");
return true;
});
回答by Roman Bondar
function SwapA(SwapActivation) {
for (i=1;i==5;i++) {
if (i != SwapActivation) {
// All Browsers supported .....
$("input[name=pg1"+i+"]").prop('disabled', true);
$("input[name=pg2"+i+"]").prop('disabled', true);
}
else {
// JQuery 1.10+ _&&_ Opera 12 and All other browsers... !!!!
if ( $("input[name=pg1"+i+"]").prop('disabled') === true)
$("input[name=pg1"+i+"]").prop('disabled', false);
if ( $("input[name=pg2"+i+"]").prop('disabled') === true)
$("input[name=pg2"+i+"]").prop('disabled', false);
// works = JQuery 1.10+ _&&_ Opera 12.16 + Firefox 24;
// do not work "Opera 17.0.1241.53", "Chrome" v.30.0.1599.101 m
$("input[name=pg1"+i+"]").removeProp('disabled');
$("input[name=pg2"+i+"]").removeProp('disabled');
// .removeProp() is affects negative
// works possible = JQuery 1.4.x :
$("input").attr('name','pg1'+i)[0].removeProp('disabled');
$("input").attr('name','pg2'+i)[0].removeProp('disabled');
}}}
is useful? :)
有用吗?:)