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

查看:68
本文介绍了使用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" />

如何也将禁用的属性添加到输入中?

How can I also add the disabled attribute to the input?

推荐答案

$("input").attr("disabled", true);截至...我不知道了.

$("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天全站免登陆