本文翻译自:How to remove “disabled” attribute using jQuery?

I have to disable inputs at first and then on click of a link to enable them. 我必须首先禁用输入,然后单击链接以启用它们。

This is what I have tried so far, but it doesn't work. 这是我到目前为止所尝试的,但它不起作用。

HTML: HTML:

<input type="text" disabled="disabled" class="inputDisabled" value="">

jQuery: jQuery的:

$("#edit").click(function(event){event.preventDefault();$('.inputDisabled').removeAttr("disabled")
});

This shows me true and then false but nothing changes for the inputs: 这显示我是true然后是false但输入没有任何变化:

$("#edit").click(function(event){alert('');event.preventDefault();alert($('.inputDisabled').attr('disabled'));$('.inputDisabled').removeAttr("disabled");alert($('.inputDisabled').attr('disabled'));
});

#1楼

参考:https://stackoom.com/question/vAsX/如何使用jQuery删除-disabled-属性


#2楼

Always use the prop() method to enable or disable elements when using jQuery (see below for why). 在使用jQuery时, 始终使用prop()方法启用或禁用元素(请参阅下面的原因)。

In your case, it would be: 在你的情况下,它将是:

$("#edit").click(function(event){event.preventDefault();$('.inputDisabled').prop("disabled", false); // Element(s) are now enabled.
});

jsFiddle example here. 这里有jsFiddle示例。


Why use prop() when you could use attr() / removeAttr() to do this? 为什么在使用attr() / removeAttr()执行此操作时使用prop()

Basically, prop() should be used when getting or setting properties (such as autoplay , checked , disabled and required amongst others). 基本上,在获取或设置属性时应使用prop() (例如autoplaycheckeddisabled和其他required )。

By using removeAttr() , you are completely removing the disabled attribute itself - while prop() is merely setting the property's underlying boolean value to false. 通过使用removeAttr() ,您将完全删除disabled属性本身 - 而prop()仅将属性的基础布尔值设置为false。

While what you want to do can be done using attr() / removeAttr() , it doesn't mean it should be done (and can cause strange/problematic behaviour, as in this case). 虽然您想要做的事情可以使用attr() / removeAttr() ,但这并不意味着它应该完成(并且可能导致奇怪/有问题的行为,如本例所示)。

The following extracts (taken from the jQuery documentation for prop() ) explain these points in greater detail: 以下摘录(摘自prop()的jQuery文档 )更详细地解释了这些要点:

"The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes." “在特定情况下,属性和属性之间的区别很重要。在jQuery 1.6之前, .attr()方法在检索某些属性时有时会考虑属性值,这可能会导致行为不一致。从jQuery 1.6开始, .prop()方法提供了一种显式检索属性值的方法,而.attr()检索属性。“

"Properties generally affect the dynamic state of a DOM element without changing the serialized HTML attribute. Examples include the value property of input elements, the disabled property of inputs and buttons, or the checked property of a checkbox. The .prop() method should be used to set disabled and checked instead of the .attr() method. The .val() method should be used for getting and setting value ." “属性通常会影响DOM元素的动态状态,而不会更改序列化的HTML属性。例如输入元素的value属性,输入和按钮的disabled属性,或复选框的checked属性.prop()方法应该用于设置disabledchecked而不是.attr()方法。应该使用.val()方法来获取和设置value 。“


#3楼

Use like this, 像这样使用,

HTML: HTML:

<input type="text" disabled="disabled" class="inputDisabled" value=""><div id="edit">edit</div>

JS: JS:

 $('#edit').click(function(){ // click to$('.inputDisabled').attr('disabled',false); // removing disabled in this class});

#4楼

<input type="text" disabled="disabled" class="inputDisabled" value="">
​<button id="edit">Edit</button>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​$("#edit").click(function(event){event.preventDefault();$('.inputDisabled').removeAttr("disabled")
});​

http://jsfiddle.net/ZwHfY/ http://jsfiddle.net/ZwHfY/


#5楼

I think you are trying to toggle the disabled state, in witch case you should use this (from this question ): 我想你正试图切换禁用状态,在这种情况下你应该使用它(来自这个问题 ):

$(".inputDisabled").prop('disabled', function (_, val) { return ! val; });

Here is a working fiddle. 这是一个工作小提琴。


#6楼

Thought this you can easily setup 想到这一点,你可以轻松设置

$(function(){
$("input[name^=radio_share]").click
(function(){if($(this).attr("id")=="radio_share_dependent"){$(".share_dependent_block input, .share_dependent_block select").prop("disabled",false);   }else{$(".share_dependent_block input, .share_dependent_block select").prop("disabled",true);   }});
});

如何使用jQuery删除“disabled”属性?相关推荐

  1. 使用jQuery设置disabled属性与移除disabled属性

    Readonly只针对input和textarea有效,而disabled对于所有的表单元素都有效,下面为大家介绍下使用jQuery设置disabled属性 表单中readOnly和disabled的 ...

  2. 使用 JavaScript 删除disabled属性

    要删除disabled属性,请选择元素并调用其 removeAttribute()上的方法,将其disabled作为参数传递,例如 btn.removeAttribute('disabled'). 该 ...

  3. jq删除某个css样式,jq删除属性_使用jquery删除css属性或样式

    摘要 腾兴网为您分享:使用jquery删除css属性或样式,中国体育,有信,相机美颜,腾讯小说等软件知识,以及来电,会动的手机壁纸,网销客,酷6,我不是我没有表情包,乐校通,搜达足球网,led滚动显示 ...

  4. s和jquery设置disabled属性为true使按钮失效

    if(cc/tr < lv){var f = confirm("生产良率小于管控良率,请确认是否提交?");if(f){$('#submit').attr("val ...

  5. html移除disabled属性,使用jQuery设置disabled属性与移除disabled属性

    表单中readOnly和disabled的区别: Readonly只针对input(text/ password)和textarea有效,而disabled对于所有的表单元素都有效,包括select, ...

  6. jquery 删除html属性值,使用jQuery的remove()和empty()方法删除Html元素及子元素和内容...

    01第1节. jQuery删除元素和内容 #JavaScript#在jQuery中,不仅可以在已有Html页面中添加新的Html元素,还可以删除已有的Html元素. 在jQuery中有2个方法可以完成 ...

  7. js,jq,jquery删除css属性

    属性值是不能删除的,只能改变属性值达到我们想要的效果 1.去掉div的高度 <div style='height:50px' id='height'></div> <sc ...

  8. jquery设置disabled属性的方法

    //两种方法设置disabled属性 $('#areaSelect').attr("disabled",true); $('#areaSelect').attr("dis ...

  9. jQuery设置disabled属性与移除disabled属性

    话不多说直接上代码! //设置disabled属性(两种方法) $('#areaSelect').attr("disabled",true); $('#areaSelect').a ...

最新文章

  1. 如何在XenServer主机上安装虚拟机
  2. boost::math::non_central_chi_squared用法的测试程序
  3. EMMC与NAND FLASH核心对比
  4. POJ 1087 -- A Plug for UNIX(最大流,建图)(文末有极限数据)
  5. How is S4 Material extened controller loaded
  6. nowcoder Forsaken的数列 fhq-treap
  7. html嵌套html解决办法(object/object)
  8. 在python中、正确的函数定义格式为_Python函数的定义与实现
  9. c#中connect函数_Flink算子使用方法及实例演示:union和connect
  10. python绘图数字_绘制一个绘图,其中Yaxis文本数据(非数字)和Xaxis数字d
  11. 使用c++制作微服务计算服务
  12. Selenium UI自动化测试(五)WebDriver控制浏览器的操作
  13. catia刨面命令_CATIA建模技巧之分割、剖切、右键属性
  14. 使用java代码打印三角形、平行四边形、菱形
  15. 主板知识详解:主板结构
  16. 软件设计师-备考知识点总结
  17. 国产FPGA(紫光同创)—— 数据采集及千兆以太网传输(一)
  18. 3D建模入门学习方法,制作过程的六个主要阶段讲解
  19. 为什么磁盘分区的时候,第一个分区前面总有一段空间(63或者2048个扇区)
  20. 企业微信和个人微信的区别报:36氪APP用户需求分析

热门文章

  1. 文档资源下载网站整理
  2. 安装旧版本chrome 浏览器方法
  3. Python中测试代码的介绍
  4. 工程伦理--7.1 可持续发展概念的起源与发展
  5. 我接触过的前端数据结构与算法
  6. 名词解释第二十六讲:热钱包
  7. 华为mate30为什么没有计算机,华为Mate30为什么没有512G版本?
  8. 防火墙_万金油_新浪博客
  9. 如何用photoshop软件更改照片像素大小并限制在指定大小下
  10. android bitmap 替换指定颜色,Android实现把bitmap图片的某一部分的颜色改成其他颜色的方法...