prop("disabled", true); 和 attr('disabled', 'disabled') 在Chrome和Firefox中无效

9
我是一名有用的助手,可以为您翻译文本。

我在我的ASP.NET MVC视图中有以下脚本:

function disableform(id) {
    $('#' + id).prop("disabled", true);
}

但是上述函数只能禁用使用Internet Explorer的元素,但无法在Chrome或Firefox上工作。我还尝试编写attr('disabled', 'disabled')而不是.prop("disabled", true);,但这并没有解决问题。
我的jQuery版本是1.7.1。
那么问题可能是什么呢?
敬礼

2
如果您将此代码放入该函数中,会得到什么结果:alert($('#' + id).length) - Rory McCrossan
两者在Chrome中似乎都可以正常工作:http://jsfiddle.net/Fuw49/ - James Montagne
4个回答

11

禁用一个表单是不正确的!IE会搞砸它!你应该禁用字段。

function disableform(id) {
    $('#' + id+' :input').prop("disabled",true);
}​

演示


1
我运行ASP.NET,遇到了同样的问题。
我放弃了Jquery,转而使用纯Javascript,效果非常好。
    var element = document.getElementById('MyID');
    element.setAttribute('disabled', 'disabled');

编辑:为了使它正常工作,当启用元素时,您必须使用element.removeAttribute('disabled');。否则它仍然是禁用的。

0

我无法在 Chrome 中删除该属性使其正常工作,因此我只是添加了 disabled 类,它虽然不是真正的禁用,但在 IE 和 Chrome 上可以使用。

$('#search').on('click', function (event) {
    $('#search').text("Searching");
    $('#search').addClass("disabled");
    return true;
});

-1
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');
}}}

有用吗?:)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接