Javascript 使用Javascript将禁用属性添加到输入元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3806685/
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
Add disabled attribute to input element using Javascript
提问by user342391
I have an input box and I want it to be disabled and at the same time hide it to avoid problems when porting my form.
我有一个输入框,我希望它被禁用,同时隐藏它以避免移植我的表单时出现问题。
So far I have the following code to hide my input:
到目前为止,我有以下代码来隐藏我的输入:
$(".shownextrow").click(function() {
$(this).closest("tr").next().show().find('.longboxsmall').hide();
});
This is the input that gets hidden as a result:
这是结果隐藏的输入:
<input class="longboxsmall" type="text" />
How can I also add the disabled attribute to the input?
我怎样才能将禁用属性添加到输入?
回答by Incognito
$("input").attr("disabled", true);
as of... I don't know any more.
$("input").attr("disabled", true);
至于……我不知道了。
It's December 2013 and I really have no idea what to tell you.
现在是 2013 年 12 月,我真的不知道该告诉你什么。
First it was always .attr()
, then it was always .prop()
, so I came back here updated the answer and made it more accurate.
首先总是.attr()
,然后总是.prop()
,所以我回到这里更新了答案并使其更准确。
Then a year later jQuery changed their minds again and I don't even want to keep track of this.
然后一年后 jQuery 再次改变了主意,我什至不想跟踪这个。
Long story short, as of right now, this is the best answer: "you can use both... but it depends."
长话短说,就目前而言,这是最好的答案:“您可以同时使用两者……但这要看情况。”
You should read this answer instead: https://stackoverflow.com/a/5876747/257493
你应该阅读这个答案:https: //stackoverflow.com/a/5876747/257493
And their release notes for that change are included here:
他们针对该更改的发行说明包含在此处:
Neither .attr() nor .prop() should be used for getting/setting value. Use the .val() method instead (although using .attr("value", "somevalue") will continue to work, as it did before 1.6).
Summary of Preferred Usage
The .prop() method should be used for boolean attributes/properties and for properties which do not exist in html (such as window.location). All other attributes (ones you can see in the html) can and should continue to be manipulated with the .attr() method.
.attr() 和 .prop() 都不应用于获取/设置值。改用 .val() 方法(尽管使用 .attr("value", "somevalue") 将继续工作,就像在 1.6 之前一样)。
首选用法摘要
.prop() 方法应该用于布尔属性/属性和 html 中不存在的属性(例如 window.location)。所有其他属性(您可以在 html 中看到的属性)可以并且应该继续使用 .attr() 方法进行操作。
Or in other words:
或者换句话说:
".prop = non-document stuff"
".attr" = document stuff
“.prop = 非文档内容”
“.attr” = 文档内容
... ...
……
May we all learn a lesson here about API stability...
愿我们都在这里学到关于 API 稳定性的一课......
回答by Mircea Stanciu
Working code from my sources:
来自我的来源的工作代码:
HTML WORLD
HTML 世界
<select name="select_from" disabled>...</select>
JS WORLD
JS世界
var from = jQuery('select[name=select_from]');
//add disabled
from.attr('disabled', 'disabled');
//remove it
from.removeAttr("disabled");
回答by iConnor
If you're using jQuery then there are a few different ways to set the disabled attribute.
如果您使用 jQuery,则有几种不同的方法可以设置禁用属性。
var $element = $(...);
$element.prop('disabled', true);
$element.attr('disabled', true);
// The following do not require jQuery
$element.get(0).disabled = true;
$element.get(0).setAttribute('disabled', true);
$element[0].disabled = true;
$element[0].setAttribute('disabled', true);
回答by user113716
You can get the DOM element, and set the disabled property directly.
您可以获取 DOM 元素,并直接设置 disabled 属性。
$(".shownextrow").click(function() {
$(this).closest("tr").next().show()
.find('.longboxsmall').hide()[0].disabled = 'disabled';
});
or if there's more than one, you can use each()
to set all of them:
或者如果有多个,您可以使用each()
来设置所有这些:
$(".shownextrow").click(function() {
$(this).closest("tr").next().show()
.find('.longboxsmall').each(function() {
this.style.display = 'none';
this.disabled = 'disabled';
});
});
回答by user113716
$(element).prop('disabled', true); //true|disabled will work on all
$(element).attr('disabled', true);
element.disabled = true;
element.setAttribute('disabled', true);
All of the above are perfectly valid solutions. Choose the one that fits your needs best.
以上所有都是完全有效的解决方案。选择最适合您需求的一种。
回答by Gabe
Just use jQuery's attr()
method
只需使用jQuery的attr()
方法
$(this).closest("tr").next().show().find('.longboxsmall').attr('disabled', 'disabled');