How to disable all <input > inside a form with jQuery?

后端 未结 8 1246
感动是毒
感动是毒 2021-01-29 22:35
....
相关标签:
8条回答
  • 2021-01-29 23:03

    The definitive answer (covering changes to jQuery api at version 1.6) has been given by Gnarf

    0 讨论(0)
  • 2021-01-29 23:05

    Above example is technically incorrect. Per latest jQuery, use the prop() method should be used for things like disabled. See their API page.

    To disable all form elements inside 'target', use the :input selector which matches all input, textarea, select and button elements.

    $("#target :input").prop("disabled", true);
    

    If you only want the elements, use this.

    $("#target input").prop("disabled", true);
    
    0 讨论(0)
  • 2021-01-29 23:05

    With this one line you can disable any input field in a form

    $('form *').prop('disabled', true);
    
    0 讨论(0)
  • 2021-01-29 23:06

    you can add

     <fieldset class="fieldset">
    

    and then you can call

     $('.fieldset').prop('disabled', true);
    
    0 讨论(0)
  • 2021-01-29 23:08

    You can do it like this:

    //HTML BUTTON
    <button type="button" onclick="disableAll()">Disable</button>
    
    //Jquery function
    function disableAll() {
        //DISABLE ALL FIELDS THAT ARE NOT DISABLED
        $('form').find(':input:not(:disabled)').prop('disabled', true);
    
        //ENABLE ALL FIELDS THAT DISABLED
        //$('form').find(':input(:disabled)').prop('disabled', false);
    }
    
    0 讨论(0)
  • 2021-01-29 23:16

    Also the more concise way is to use their selectors engine. So to disable all form elements in a div or form parent.

    $myForm.find(':input:not(:disabled)').prop('disabled',true)
    
    0 讨论(0)
提交回复
热议问题