admin管理员组文章数量:1640624
本文翻译自:How to remove “disabled” attribute using jQuery?
I have to disable inputs at first and then on click of a link to enable them. 我必须首先禁用输入,然后单击链接以启用它们。
This is what I have tried so far, but it doesn't work. 这是我到目前为止所尝试的,但它不起作用。
HTML: HTML:
<input type="text" disabled="disabled" class="inputDisabled" value="">
jQuery: jQuery的:
$("#edit").click(function(event){
event.preventDefault();
$('.inputDisabled').removeAttr("disabled")
});
This shows me true
and then false
but nothing changes for the inputs: 这显示我是true
然后是false
但输入没有任何变化:
$("#edit").click(function(event){
alert('');
event.preventDefault();
alert($('.inputDisabled').attr('disabled'));
$('.inputDisabled').removeAttr("disabled");
alert($('.inputDisabled').attr('disabled'));
});
#1楼
参考:https://stackoom/question/vAsX/如何使用jQuery删除-disabled-属性
#2楼
Always use the prop()
method to enable or disable elements when using jQuery (see below for why). 在使用jQuery时, 始终使用prop()
方法启用或禁用元素(请参阅下面的原因)。
In your case, it would be: 在你的情况下,它将是:
$("#edit").click(function(event){
event.preventDefault();
$('.inputDisabled').prop("disabled", false); // Element(s) are now enabled.
});
jsFiddle example here. 这里有jsFiddle示例。
Why use
prop()
when you could useattr()
/removeAttr()
to do this? 为什么在使用attr()
/removeAttr()
执行此操作时使用prop()
?
Basically, prop()
should be used when getting or setting properties (such as autoplay
, checked
, disabled
and required
amongst others). 基本上,在获取或设置属性时应使用prop()
(例如autoplay
, checked
, disabled
和其他required
)。
By using removeAttr()
, you are completely removing the disabled
attribute itself - while prop()
is merely setting the property's underlying boolean value to false. 通过使用removeAttr()
,您将完全删除disabled
属性本身 - 而prop()
仅将属性的基础布尔值设置为false。
While what you want to do can be done using attr()
/ removeAttr()
, it doesn't mean it should be done (and can cause strange/problematic behaviour, as in this case). 虽然您想要做的事情可以使用attr()
/ removeAttr()
,但这并不意味着它应该完成(并且可能导致奇怪/有问题的行为,如本例所示)。
The following extracts (taken from the jQuery documentation for prop() ) explain these points in greater detail: 以下摘录(摘自prop()的jQuery文档 )更详细地解释了这些要点:
"The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the
.attr()
method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the.prop()
method provides a way to explicitly retrieve property values, while.attr()
retrieves attributes." “在特定情况下,属性和属性之间的区别很重要。在jQuery 1.6之前,.attr()
方法在检索某些属性时有时会考虑属性值,这可能会导致行为不一致。从jQuery 1.6开始,.prop()
方法提供了一种显式检索属性值的方法,而.attr()
检索属性。“"Properties generally affect the dynamic state of a DOM element without changing the serialized HTML attribute. Examples include the
value
property of input elements, thedisabled
property of inputs and buttons, or thechecked
property of a checkbox. The.prop()
method should be used to setdisabled
andchecked
instead of the.attr()
method. The.val()
method should be used for getting and settingvalue
." “属性通常会影响DOM元素的动态状态,而不会更改序列化的HTML属性。例如输入元素的value
属性,输入和按钮的disabled
属性,或复选框的checked
属性.prop()
方法应该用于设置disabled
和checked
而不是.attr()
方法。应该使用.val()
方法来获取和设置value
。“
#3楼
Use like this, 像这样使用,
HTML: HTML:
<input type="text" disabled="disabled" class="inputDisabled" value="">
<div id="edit">edit</div>
JS: JS:
$('#edit').click(function(){ // click to
$('.inputDisabled').attr('disabled',false); // removing disabled in this class
});
#4楼
<input type="text" disabled="disabled" class="inputDisabled" value="">
<button id="edit">Edit</button>
$("#edit").click(function(event){
event.preventDefault();
$('.inputDisabled').removeAttr("disabled")
});
http://jsfiddle/ZwHfY/ http://jsfiddle/ZwHfY/
#5楼
I think you are trying to toggle the disabled state, in witch case you should use this (from this question ): 我想你正试图切换禁用状态,在这种情况下你应该使用它(来自这个问题 ):
$(".inputDisabled").prop('disabled', function (_, val) { return ! val; });
Here is a working fiddle. 这是一个工作小提琴。
#6楼
Thought this you can easily setup 想到这一点,你可以轻松设置
$(function(){
$("input[name^=radio_share]").click
(
function()
{
if($(this).attr("id")=="radio_share_dependent")
{
$(".share_dependent_block input, .share_dependent_block select").prop("disabled",false);
}
else
{
$(".share_dependent_block input, .share_dependent_block select").prop("disabled",true);
}
}
);
});
版权声明:本文标题:如何使用jQuery删除“disabled”属性? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1729317370a1195461.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论