disable all form elements inside div

后端 未结 12 1764
栀梦
栀梦 2020-12-04 08:39

is there a way to disable all fields (textarea/textfield/option/input/checkbox/submit etc) in a form by telling only the parent div name in jquery/javascript?

相关标签:
12条回答
  • 2020-12-04 08:43

    Following will disable all the input but will not able it to btn class and also added class to overwrite disable css.

    $('#EditForm :input').not('.btn').attr("disabled", true).addClass('disabledClass');
    

    css class

    .disabledClass{
      background-color: rgb(235, 235, 228) !important;
    }
    
    0 讨论(0)
  • 2020-12-04 08:49

    How about achieving this using only HTML attribute 'disabled'

    <form>
     <fieldset disabled>
      <div class="row">
       <input type="text" placeholder="">
       <textarea></textarea>
       <select></select>
      </div>
      <div class="pull-right">
        <button class="button-primary btn-sm" type="submit">Submit</button>
      </div>
     </fieldset>
    </form>
    

    Just by putting disabled in the fieldset all the fields inside of that fieldset get disabled.

    $('fieldset').attr('disabled', 'disabled');
    
    0 讨论(0)
  • 2020-12-04 08:52

    For jquery 1.6+, use .prop() instead of .attr(),

    $("#parent-selector :input").prop("disabled", true);
    

    or

    $("#parent-selector :input").attr("disabled", "disabled");
    
    0 讨论(0)
  • 2020-12-04 08:52

    If your form inside div simply contains form inputting elements, then this simple query will disable every element inside form tag:

    <div id="myForm">
        <form action="">
        ...
        </form>
    </div>
    

    However, it will also disable other than inputting elements in form, as it's effects will only be seen on input type elements, therefore suitable majorly for every type of forms!

    $('#myForm *').attr('disabled','disabled');
    
    0 讨论(0)
  • 2020-12-04 08:56

    I'm using the function below at various points. Works in a div or button elements in a table as long as the right selector is used. Just ":button" would not re-enable for me.

    function ToggleMenuButtons(bActivate) {
        if (bActivate == true) {
            $("#SelectorId :input[type='button']").prop("disabled", true);
        } else {
            $("#SelectorId :input[type='button']").removeProp("disabled");
        }
    }
    
    0 讨论(0)
  • 2020-12-04 08:57

    For me the accepted answer did not work as I had some asp net hidden fields which got disabled as well so I chose only to disable visible fields

    //just to save the list so we can enable fields later
    var list = [];
    $('#parent-selector').find(':input:visible:not([readonly][disabled]),button').each(function () {
        list.push('#' + this.id);
    });
    
    $(list.join(',')).attr('readonly', true);
    
    0 讨论(0)
提交回复
热议问题