jQuery 如何使用jQuery禁用表单内的所有<input>?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1416900/
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-08-26 11:24:41  来源:igfitidea点击:

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

jqueryforms

提问by omg

<form id="target">
....
</form>

回答by cletus

In older versions you could use attr. As of jQuery 1.6 you should use propinstead:

在旧版本中,您可以使用attr. 从 jQuery 1.6 开始,您应该prop改用:

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

To disable all form elements inside 'target'. See :input:

禁用“目标”内的所有表单元素。见:input

Matches all input, textarea, select and button elements.

匹配所有输入、文本区域、选择和按钮元素。

If you only want the <input>elements:

如果你只想要<input>元素:

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

回答by MetaSkills

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

上面的例子在技术上是不正确的。根据最新的 jQuery,使用该prop()方法应该用于诸如禁用之类的事情。请参阅他们的 API 页面。

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

要禁用 'target' 内的所有表单元素,请使用匹配所有输入、文本区域、选择和按钮元素的 :input 选择器。

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

If you only want the elements, use this.

如果您只想要元素,请使用它。

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

回答by MetaSkills

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

更简洁的方法是使用他们的选择器引擎。因此要禁用 div 或表单父级中的所有表单元素。

$myForm.find(':input:not(:disabled)').prop('disabled',true)

回答by Otto Kanellis

you can add

你可以加

 <fieldset class="fieldset">

and then you can call

然后你可以打电话

 $('.fieldset').prop('disabled', true);

回答by Samir Rahimy

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

通过这一行,您可以禁用表单中的任何输入字段

$('form *').prop('disabled', true);

回答by Angel Cuenca

To disable all form, as easy as write:

要禁用所有表单,就像写一样简单:

jQuery 1.6+

jQuery 1.6+

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

jQuery 1.5 and below

jQuery 1.5 及以下

$("#form :input").attr('disabled','disabled');

回答by ErichBSchulz

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

Gnarf给出了明确的答案(涵盖 1.6 版对 jQuery api 的更改)