Add disabled attribute to input element using Javascript(使用Javascript向输入元素添加禁用属性)
问题描述
我有一个输入框,我希望它被禁用,同时隐藏它以避免在移植我的表单时出现问题.
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();
});
这是被隐藏的输入:
<input class="longboxsmall" type="text" />
如何将 disabled 属性也添加到输入中?
How can I also add the disabled attribute to the input?
推荐答案
$("input").attr("disabled", true);
as of... I don't不知道了.
$("input").attr("disabled", true);
as of... I don't know any more.
现在是 2013 年 12 月,我真的不知道该告诉你什么.
It's December 2013 and I really have no idea what to tell you.
首先它总是 .attr()
,然后它总是 .prop()
,所以我回到这里更新了答案并使其更准确.
First it was always .attr()
, then it was always .prop()
, so I came back here updated the answer and made it more accurate.
一年后,jQuery 再次改变了主意,我什至不想跟踪这一点.
Then a year later jQuery changed their minds again and I don't even want to keep track of this.
长话短说,就目前而言,这是最好的答案:您可以同时使用两者……但这取决于情况."
Long story short, as of right now, this is the best answer: "you can use both... but it depends."
您应该改为阅读此答案:https://stackoverflow.com/a/5876747/257493
You should read this answer instead: https://stackoverflow.com/a/5876747/257493
此处包含他们对该更改的发行说明:
And their release notes for that change are included here:
.attr() 和 .prop() 都不应该用于获取/设置值.请改用 .val() 方法(尽管使用 .attr("value", "somevalue") 将继续工作,就像 1.6 之前一样).
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).
首选用法摘要
.prop() 方法应该用于布尔属性/属性以及 html 中不存在的属性(例如 window.location).所有其他属性(您可以在 html 中看到的属性)可以并且应该继续使用 .attr() 方法进行操作.
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.
或者换句话说:
.prop = 非文档内容"
".prop = non-document stuff"
.attr"= 文档资料
".attr" = document stuff
......
愿我们都在这里学到关于 API 稳定性的教训...
May we all learn a lesson here about API stability...
这篇关于使用Javascript向输入元素添加禁用属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用Javascript向输入元素添加禁用属性
基础教程推荐
- 直接将值设置为滑块 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01