.attr(“禁用”,“禁用”)问题

我有这个function从输入字段切换禁用属性:

$('.someElement').click(function(){ if (someCondition) { console.log($target.prev('input')) // gives out the right object $target.toggleClass('open').prev('input').attr('disabled', 'disabled'); }else{ $target.toggleClass('open').prev('input').removeAttr('disabled'); //this works } }) 

removeAttr工作正常,但当我需要再次添加禁用时,它什么也没做。 我的console.log被触发(并返回正确的输入字段),所以我确信我的if语句有效。 但是当我在firefox中使用firebug检查DOM时,不会显示disabled属性。

有人能帮我吗?

PS:请不要专注于函数或if语句本身,正常工作只是那个不适用于残疾的attr …

编辑 :它的输入类型=“隐藏”是否可能禁用对隐藏字段不起作用?

谢谢大家的贡献! 我发现了问题:

它是一个消防BUG !!!

我的代码有效。 我已经要求PHP Dev将隐藏的输入类型更改为输入类型文本。 禁用function有效。 但是firebug控制台没有更新这个状态!

你可以在http://jsbin.com/uneti3/3#自己测试这个firebug bug。 对于示例页面,请访问aSeptik。

更新:2。2012年6月:FF11中的Firebug仍有此错误。

更新

演示: http : //jsbin.com/uneti3/3

你的代码是错的,它应该是这样的:

  $(bla).click(function() { var disable = $target.toggleClass('open').hasClass('open'); $target.prev().prop("disabled", disable); }); 

你以错误的方式使用toggleClass函数

试试这个更新的代码:

 $(bla).click(function(){ if (something) { console.log($target.prev("input")) // gives out the right object $target.toggleClass("open").prev("input").attr("disabled", "true"); }else{ $target.toggleClass("open").prev("input").removeAttr("disabled"); //this works } }) 

切换禁用按钮状态时,我遇到了类似的问题! 触发removeProp('disabled')该按钮拒绝再次“禁用”! 我发现了一个有趣的解决方案:使用prop("disabled",true)禁用按钮和prop("disabled",false)重新启用它! 现在,我能够多次切换按钮的“禁用”状态! 试试看。

 $("#vp_code").textinput("enable"); $("#vp_code").textinput("disable"); 

你可以尝试一下

尝试

 $(bla).click(function(){ if (something) { console.log("A:"+$target.prev("input")) // gives out the right object $target.toggleClass("open").prev("input").attr("disabled", "disabled"); }else{ console.log("A:"+$target.prev("input")) // any thing from there for a single click? $target.toggleClass("open").prev("input").removeAttr("disabled"); //this works } }); 

添加禁用的属性

 $('#id').attr("disabled", "true"); 

删除已禁用的属性

 $('#id').removeAttr('disabled');