使用Javascript向输入元素添加禁用属性 [英] Add disabled attribute to input element using Javascript

查看:19
本文介绍了使用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向输入元素添加禁用属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆