How to disable/enable input field on click with jQuery

后端 未结 7 749
慢半拍i
慢半拍i 2020-12-31 13:36

How to properly enable/disable input field on click with jQuery?

I was experimenting with:

$(\"#FullName\").removeAttr(\'disabled\');
相关标签:
7条回答
  • 2020-12-31 14:09

    For jQuery version 1.6+ use prop:

    $('#elementId').click(function(){
            $('#FullName').prop('disabled', true\false);
    });
    

    For older versions of jQuery use attr:

    $('#elementId').click(function(){
            $('#FullName').attr('disabled', 'disabled'\'');
    });
    
    0 讨论(0)
  • 2020-12-31 14:09

    This should do it.

    $("#FullName").attr('disabled', 'disabled');
    

    Shiplu is correct, but use this if you have are not using jquery 1.6+

    0 讨论(0)
  • 2020-12-31 14:12
    $("#anOtherButton").click(function(){
      $("#FullName").attr('disabled', 'disabled'); 
     });
    

    • .attr( attributeName, value) function

      Set one or more attributes for the set of matched elements

    0 讨论(0)
  • 2020-12-31 14:26
    $("#FullName").prop('disabled', true);
    

    Will do.

    But keep in mind after you disable it (by the above code) onclick handler wont work as its disabled. To enable it again add $("#FullName").removeAttr('disabled'); in the onclick handler of another button or field.

    0 讨论(0)
  • 2020-12-31 14:32

    another simple method to enable/disable input feild

    $("#anOtherButton").click(function() {
      $("#FullName").attr('disabled', !$("#FullName").attr('disabled'));
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    
    <input id="FullName" style="width: 299px" value="Marko" disabled="disabled" />
    <br>
    <br>
    <input type="button" id="anOtherButton" value="disable/enable" />

    0 讨论(0)
  • 2020-12-31 14:33
    $('#checkbox-id').click(function()
    {
        //If checkbox is checked then disable or enable input
        if ($(this).is(':checked'))
        {
            $("#to-enable-input").removeAttr("disabled"); 
            $("#to-disable-input").attr("disabled","disabled");
        }
        //If checkbox is unchecked then disable or enable input
        else
        {
            $("#to-enable-input").removeAttr("disabled"); 
            $("#to-disable-input").attr("disabled","disabled");
        }
    });
    
    0 讨论(0)
提交回复
热议问题