简体   繁体   English

禁用div内的所有表单元素

[英]disable all form elements inside div

有没有办法通过只告诉 jquery/javascript 中的父 div 名称来禁用表单中的所有字段(textarea/textfield/option/input/checkbox/submit 等)?

尝试使用:input选择器和父选择器:

$("#parent-selector :input").attr("disabled", true);
$('#mydiv').find('input, textarea, button, select').attr('disabled','disabled');

For jquery 1.6+, use .prop() instead of .attr() ,对于 jquery .prop() ,使用.prop()而不是.attr()

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

or或者

$("#parent-selector :input").attr("disabled", "disabled");
    $(document).ready(function () {
        $('#chkDisableEnableElements').change(function () {
            if ($('#chkDisableEnableElements').is(':checked')) {
                enableElements($('#divDifferentElements').children());
            }
            else {
                disableElements($('#divDifferentElements').children());
            }
        });
    });

    function disableElements(el) {
        for (var i = 0; i < el.length; i++) {
            el[i].disabled = true;

            disableElements(el[i].children);
        }
    }

    function enableElements(el) {
        for (var i = 0; i < el.length; i++) {
            el[i].disabled = false;

            enableElements(el[i].children);
        }
    }

只需这行代码将禁用所有输入元素

$('#yourdiv *').prop('disabled', true);

How about achieving this using only HTML attribute 'disabled'如何仅使用 HTML 属性“禁用”来实现这一点

<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.只需将 disabled 放在 fieldset 中,该 fieldset 内的所有字段都会被禁用。

$('fieldset').attr('disabled', 'disabled');

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.只要使用正确的选择器,就可以在表格中的 div 或按钮元素中使用。 Just ":button" would not re-enable for me.只是 ":button" 不会为我重新启用。

function ToggleMenuButtons(bActivate) {
    if (bActivate == true) {
        $("#SelectorId :input[type='button']").prop("disabled", true);
    } else {
        $("#SelectorId :input[type='button']").removeProp("disabled");
    }
}

Following will disable all the input but will not able it to btn class and also added class to overwrite disable css.以下将禁用所有输入,但无法将其添加到 btn 类,并且还添加了类来覆盖禁用 css。

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

css class css类

.disabledClass{
  background-color: rgb(235, 235, 228) !important;
}

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对我来说,接受的答案不起作用,因为我有一些 asp 网络隐藏字段也被禁用,所以我选择只禁用可见字段

//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);

Use the CSS Class to prevent from Editing the Div Elements使用 CSS 类防止编辑 Div 元素

CSS: CSS:

.divoverlay
{
position:absolute;
width:100%;
height:100%;
background-color:transparent;
z-index:1;
top:0;
}

JS: JS:

$('#divName').append('<div class=divoverlay></div>');

Or add the class name in HTML Tag.或者在 HTML 标签中添加类名。 It will prevent from editing the Div Elements.它将阻止编辑 Div 元素。

Only text type仅文本类型

$(".form-edit-account :input[type=text]").attr("disabled", "disabled");

Only Password type仅密码类型

$(".form-edit-account :input[type=password]").attr("disabled", "disabled");

Only Email Type仅电子邮件类型

$(".form-edit-account :input[type=email]").attr("disabled", "disabled");

If your form inside div simply contains form inputting elements, then this simple query will disable every element inside form tag:如果div中的表单只包含表单输入元素,那么这个简单的查询将禁用form标签中的每个元素:

<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!但是,它也会禁用form输入元素以外的其他元素,因为它的效果只会在输入类型元素上看到,因此主要适用于每种类型的表单!

$('#myForm *').attr('disabled','disabled');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM