Jak wyłączyć wszystko w formularzu za pomocą jQuery?

<form id="target">
....
</form>
 121
Author: omg, 2009-09-13

7 answers

W starszych wersjach można użyć attr. Od jQuery 1.6 należy używać prop zamiast:

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

Aby wyłączyć wszystkie elementy formularza wewnątrz 'target'. Zobacz też :input:

Pasuje do wszystkich elementów input, textarea, select I button.

Jeśli chcesz tylko <input> elementów:

$("#target input").prop("disabled", true);
 233
Author: cletus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-10 08:59:48

Powyższy przykład jest niepoprawny technicznie. W najnowszym jQuery należy użyć metody prop() Do Rzeczy Takich jak wyłączone. Zobacz ich stronę API.

Aby wyłączyć wszystkie elementy formularza wewnątrz 'target', użyj selektora: input, który pasuje do wszystkich elementów input, textarea, select I button.

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

Jeśli chcesz tylko elementy, użyj tego.

$("#target input").prop("disabled", true);
 36
Author: MetaSkills,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-30 18:19:26

Również bardziej zwięzłym sposobem jest użycie ich silnika selektorów. Aby więc wyłączyć wszystkie elementy formularza w div lub rodzicu formularza.

$myForm.find(':input:not(:disabled)').prop('disabled',true)
 13
Author: MetaSkills,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-30 10:11:01

Aby wyłączyć wszystkie formy, wystarczy napisać:

JQuery 1.6+

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

JQuery 1.5 i poniżej

$("#form :input").attr('disabled','disabled');
 6
Author: Angel Cuenca,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-18 17:46:31

Możesz dodać

 <fieldset class="fieldset">

I wtedy możesz zadzwonić

 $('.fieldset').prop('disabled', true);
 6
Author: Otto Kanellis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-15 09:54:24

Za pomocą tej jednej linii możesz wyłączyć dowolne pole wejściowe w postaci

$('form *').prop('disabled', true);
 1
Author: Samir Rahimy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-11 12:51:14

Ostateczna odpowiedź (dotycząca zmian w jQuery api w wersji 1.6) została udzielona przez Gnarf

 -1
Author: ErichBSchulz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:33:32