javascript 使用 jQuery 在表单中禁用除某些元素之外的所有输入元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29078231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:03:53  来源:igfitidea点击:

Disabling all input elements except some in a form using jQuery

javascriptjqueryhtml

提问by Jason

I am trying to disable almost all input elements in a form using jquery, but I need some input elements to be enabled. For example:

我正在尝试使用 jquery 禁用表单中的几乎所有输入元素,但我需要启用一些输入元素。例如:

$(document).ready(function () {
    $("#document :input[name!='tloEnable']).attr("disabled", true);
});

This works great on the elements I have with the same name 'tloEnable'. However there are a few other elements that have different name attributes (filename, notifyUsers, notifyTeam). How can I include them too while disabling the remaining input elements?

这对我具有相同名称“tloEnable”的元素非常有效。但是,还有一些其他元素具有不同的名称属性(文件名、notifyUsers、notifyTeam)。如何在禁用其余输入元素的同时也包含它们?

$(document).ready(function () {
    $("#document :input[name!='tloEnable], [name!='filename'], [name!='notifyUsers'], [name!='notifyTeam']).attr("disabled", true);
});

回答by Salman A

Use the .not() functionand pass a selector; matched elements will be excluded:

使用.not() function并传递一个选择器;匹配的元素将被排除:

$(document).ready(function () {
    $(":input").not("[name=tloEnable], [name=filename], [name=notifyUsers]")
        .prop("disabled", true);
});

The :not() selectorworks the same way:

:not() selector作品以同样的方式:

$(document).ready(function () {
    $(":input:not([name=tloEnable], [name=filename], [name=notifyUsers])")
        .prop("disabled", true);
});

回答by Carl Reid

Give the inputs you wish to disable a class such as disabled-inputs. Then simply:

提供您希望禁用类的输入,例如disabled-inputs. 然后简单地:

Jquery 1.6:

jQuery 1.6:

$(".disabled-inputs").prop('disabled', true);  

jQuery 1.5 and below:

jQuery 1.5 及以下:

$(".disabled-inputs").attr('disabled','disabled');

回答by Moob

Salman A's solution is the one I would probably go with(assuming you can't just give the fields a classname) but you could also consider using jQuery's filter()to only select elements that match your criteria.

Salman A 的解决方案是我可能会采用的解决方案(假设您不能只为字段指定类名),但您也可以考虑使用jQueryfilter()来仅选择符合您的条件的元素。

Here we're just checking that namedoes not exist in our list of fieldsNotToBeDisabledbut you could easily extend it to test for anything else.

在这里,我们只是检查name列表中不存在的内容,fieldsNotToBeDisabled但您可以轻松扩展它以测试其他任何内容。

var fieldsNotToBeDisabled = new Array("tloEnable", "filename", "notifyUsers", "notifyTeam");

$("form input").filter(function(index){
    return fieldsNotToBeDisabled.indexOf($(this).attr("name"))<0;
}).prop("disabled", true);
input {border:1px solid green; }
input:disabled { border-color: red; background:#eee;}
<form>
    <input name="tloEnable" />
    <input name="filename" />
    <input name="notifyUsers" />
    <input name="notifyTeam" />
    <input name="testa" />
    <input name="testb" />
    <input name="testc" />
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>