Unable to enable disabled input

This question already has an answer here:

  • Disable/enable an input with jQuery? 11 answers

  • Just to expand on the answer...

    The real problem is that you are trying to set disabled to false via setAttribute() which doesn't behave as you are expecting. An element is disabled if the disabled-attribute is set, regardless of value so, disabled="true" , disabled="disabled" and disabled="false" are all equivalent: the element gets disabled).

    You should instead remove the complete attribute

    $('#enb').click(function() {
       $('#inp').removeAttr('disabled');
    });
    

    只要删除disabled属性

    $('#enb').click(function() {
       $('#inp').removeAttr('disabled');
    });
    

    The issue was with your code having true and false written in quotes, making them strings instead of booleans. Note that both 'true' and 'false' are truthy values, and thus your code set the enabled value to true in both cases.

    I expect the following will work for you, check the snippet as well:

    $('#dis').click(function() {
        $("#inp").prop('disabled', true);
    });
    
    $('#enb').click(function() {
        $("#inp").prop('disabled', false);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <input id='inp' type='text'>
    <button id='enb'>Enable</button>
    <button id='dis'>Disable</button>
    链接地址: http://www.djcxy.com/p/22996.html

    上一篇: 如何使文本框启用和禁用jQuery中

    下一篇: 无法启用禁用的输入