2010-09-27 135 views
94

我有一个输入框,我希望它被禁用,并同时隐藏它,以避免移植我的窗体时出现问题。使用JavaScript将禁用的属性添加到输入元素

到目前为止,我有下面的代码隐藏我输入:

$(".shownextrow").click(function() { 
    $(this).closest("tr").next().show().find('.longboxsmall').hide(); 
}); 

这是一个被隐藏的结果输入:

<input class="longboxsmall" type="text" /> 

如何我也禁用属性添加到输入?

回答

218

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

这是2013年12月,我真的不知道该告诉你什么。

首先它总是.attr(),然后它总是.prop(),所以我回到这里更新了答案并使其更加准确。

然后一年后,jQuery又改变了主意,我甚至不想跟踪这一点。长话短说,截至目前,这是最好的答案:“你可以同时使用......但这取决于你。”

你应该阅读这个答案,而不是:https://stackoverflow.com/a/5876747/257493

而他们的发行说明这种变化包括在这里:

无论.attr(),也不.prop()应该被用于获取/设置值。使用.val()方法(尽管使用.attr(“value”,“somevalue”)将继续工作,就像1.6之前那样)。

总结优选的用法

的.prop()方法的应该用于布尔属性/特性和对于不以html存在性能(如window.location的)。所有其他属性(您可以在html中看到的)可以并应该继续使用.attr()方法进行操作。

或者换句话说:

“.prop =非文件类的东西”

“.attr”=文件的东西

... ...

我们都可以在这里了解关于API稳定性的教训...

+7

+1进行更新。谢谢。 – rie819 2012-06-26 13:09:24

+3

+1使更新文本足够大,让我注意 – 2013-12-06 18:13:54

+0

@VelVVictus没有那么快。我很抱歉地说,在我发布这一年后他们再次改变了它...我忘记了这个答案。阅读此答案:http://stackoverflow.com/a/5876747/257493 – Incognito 2013-12-06 18:24:19

2

只是使用jQuery的attr()方法

$(this).closest("tr").next().show().find('.longboxsmall').attr('disabled', 'disabled'); 
+0

并使用'.removeAttr('disabled');' – ruhong 2013-12-29 21:16:00

10

你可以得到的DOM元素,并直接设置禁用属性。

$(".shownextrow").click(function() { 
    $(this).closest("tr").next().show() 
      .find('.longboxsmall').hide()[0].disabled = 'disabled'; 
}); 

,或者如果有一个以上的,可以使用each()来设置所有的人:

$(".shownextrow").click(function() { 
    $(this).closest("tr").next().show() 
      .find('.longboxsmall').each(function() { 
       this.style.display = 'none'; 
       this.disabled = 'disabled'; 
      }); 
}); 
5
$(element).prop('disabled', true); //true|disabled will work on all 
$(element).attr('disabled', true); 
element.disabled = true; 
element.setAttribute('disabled', true); 

以上所有的都是非常有效的解决方案。选择最适合您需求的产品。

+0

删除禁用状态这里的实际问题是“我怎样才能将禁用的属性添加到输入?”关键字是“也”。意思是OP知道如何去做其余的事情,实际上他要求的是如何设置disabled属性。 – user1596138 2013-12-06 18:35:21

10

如果您使用的是jQuery,那么有几种不同的方法来设置disabled属性。从我的消息来源

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); 
+0

'$ element.eq(0)[0] .disabled = true;'? :-P – qwertynl 2013-12-06 18:39:31

+1

是的,这对我来说是先进的,我不会为了提高性能而走得太远。 – iConnor 2013-12-06 18:40:19

+0

+1。虽然它有点多余。如果你打算处理一个NodeList /数组元素,那么通过索引来选择它们是很愚蠢的。迭代或者仅仅选择你需要的元素会更有意义。 – user1596138 2013-12-06 19:02:47

32

工作代码:

HTML WORLD

<select name="select_from" disabled>...</select> 

JS世界

var from = jQuery('select[name=select_from]'); 

//add disabled 
from.attr('disabled', 'disabled'); 



//remove it 
from.removeAttr("disabled"); 
+1

至于人们来到这里,从谷歌,直接从[jQuery的]的说明(http://api.jquery.com/attr/#attr1):'在jQuery 1.6中,.attr()方法的属性返回undefined那尚未设置。检索和修改DOM属性,如检查,选定,或元素形式的元素禁用状态,使用.prop()method' – 2016-08-10 09:12:14

相关问题