Disable/enable an input with jQuery?

后端 未结 18 2647
礼貌的吻别
礼貌的吻别 2020-11-21 07:47
$input.disabled = true;

or

$input.disabled = \"disabled\";

Which is the standard way? And, conversely, how do yo

相关标签:
18条回答
  • 2020-11-21 07:53

    2018, without JQuery (ES6)

    Disable all input:

    [...document.querySelectorAll('input')].map(e => e.disabled = true);
    

    Disable input with id="my-input"

    document.getElementById('my-input').disabled = true;
    

    The question is with JQuery, it's just FYI.

    0 讨论(0)
  • 2020-11-21 07:54

    If you just want to invert the current state (like a toggle button behaviour):

    $("input").prop('disabled', ! $("input").prop('disabled') );
    
    0 讨论(0)
  • 2020-11-21 07:57

    Disable true for input type :

    In case of a specific input type (Ex. Text type input)

    $("input[type=text]").attr('disabled', true);
    

    For all type of input type

    $("input").attr('disabled', true);
    
    0 讨论(0)
  • 2020-11-21 07:59
        // Disable #x
        $( "#x" ).prop( "disabled", true );
        // Enable #x
        $( "#x" ).prop( "disabled", false );
    

    Sometimes you need to disable/enable the form element like input or textarea. Jquery helps you to easily make this with setting disabled attribute to "disabled". For e.g.:

      //To disable 
      $('.someElement').attr('disabled', 'disabled');
    

    To enable disabled element you need to remove "disabled" attribute from this element or empty it's string. For e.g:

    //To enable 
    $('.someElement').removeAttr('disabled');
    
    // OR you can set attr to "" 
    $('.someElement').attr('disabled', '');
    

    refer :http://garmoncheg.blogspot.fr/2011/07/how-to-disableenable-element-with.html

    0 讨论(0)
  • 2020-11-21 08:00

    Use like this,

     $( "#id" ).prop( "disabled", true );
    
     $( "#id" ).prop( "disabled", false );
    
    0 讨论(0)
  • 2020-11-21 08:03
    <html>
    <body>
    
    Name: <input type="text" id="myText">
    
    
    
    <button onclick="disable()">Disable Text field</button>
    <button onclick="enable()">Enable Text field</button>
    
    <script>
    function disable() {
        document.getElementById("myText").disabled = true;
    }
    function enable() {
        document.getElementById("myText").disabled = false;
    }
    </script>
    
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题