jQuery .attr("disabled", "disabled") 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3004885/
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
.attr("disabled", "disabled") issue
提问by meo
I have this function toggles the disabled attribute form a input field:
我有这个功能从输入字段切换禁用属性:
$('.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
}
})
the removeAttr
works fine but when i need to add the disabled again it does just nothing. My console.log is triggered (and giving me back the right input field) so I'm sure that my if statement works. But when I inspect the DOM with firebug in firefox, the disabled attribute does not appear.
该removeAttr
工作正常,但是当我需要的禁用再次添加它根本不值一提。我的 console.log 被触发(并返回正确的输入字段),所以我确定我的 if 语句有效。但是当我在 firefox 中用 firebug 检查 DOM 时,没有出现 disabled 属性。
can someone help me?
有人能帮我吗?
PS: please don't focus on the function or the if statement itself, works fine its just that attr that does not work for disabled...
PS:请不要关注函数或 if 语句本身,工作正常,只是 attr 不适用于残疾人......
edit: its an input type="hidden" is it possible that disabled does not work on hidden fields?
编辑:它的输入类型=“隐藏”是否有可能禁用对隐藏字段不起作用?
回答by meo
Thank you all for your contribution! I found the problem:
感谢大家的贡献!我发现了问题:
ITS A FIREBUG BUG !!!
这是一个火虫错误!!!
My code works. I have asked the PHP Dev to change the input types hidden in to input type text. The disabled feature works. But the firebug console does not update this status!
我的代码有效。我已经要求 PHP Dev 将隐藏的输入类型更改为输入类型文本。禁用功能有效。但是萤火虫控制台不会更新此状态!
you can test out this firebug bug by your self here http://jsbin.com/uneti3/3#. Thx to aSeptik for the example page.
你可以在这里http://jsbin.com/uneti3/3#自己测试这个萤火虫错误。感谢示例页面的 aSeptik。
update: 2. June 2012: Firebug in FF11 still has this bug.
更新:2. 2012 年 6 月:FF11 中的 Firebug 仍然存在此错误。
回答by Luca Filosofi
UPDATED
更新
DEMO: http://jsbin.com/uneti3/3
your code is wrong, it should be something like this:
你的代码错了,应该是这样的:
$(bla).click(function() {
var disable = $target.toggleClass('open').hasClass('open');
$target.prev().prop("disabled", disable);
});
you are using the toggleClass function in wrong way
您以错误的方式使用 toggleClass 函数
回答by Pranay Rana
Try this updated code :
试试这个更新的代码:
$(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
}
})
回答by jin
I was facing the similar issue while toggling the disabled state of button! After firing the removeProp('disabled')
the button refused to get "disabled" again! I found an interesting solution : use prop("disabled",true)
to disable the button and prop("disabled",false)
to re-enable it!
Now I was able to toggle the "disabled" state of my button as many times I needed! Try it out.
我在切换按钮的禁用状态时遇到了类似的问题!触发removeProp('disabled')
按钮后拒绝再次“禁用”!我找到了一个有趣的解决方案:用于prop("disabled",true)
禁用按钮并prop("disabled",false)
重新启用它!现在,我可以根据需要多次切换按钮的“禁用”状态!试试看。
回答by sinda
$("#vp_code").textinput("enable");
$("#vp_code").textinput("disable");
you can try it
你可以试试看
回答by Dipendra Ghatal
To add disabled attribute
添加禁用属性
$('#id').attr("disabled", "true");
To remove Disabled Attribute
删除禁用属性
$('#id').removeAttr('disabled');
回答by Dennis C
Try
尝试
$(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
}
});