Wyłączyć / włączyć wejście z jQuery?

$input.disabled = true;

Lub

$input.disabled = "disabled";

Jaki jest standardowy sposób? I odwrotnie, jak włączyć wyłączone wejście?

Author: John Slegers, 2009-09-12

15 answers

JQuery 1.6 +

Aby zmienić właściwość disabled należy użyć .prop() funkcja.

$("input").prop('disabled', true);
$("input").prop('disabled', false);

JQuery 1.5 i poniżej

Funkcja .prop() nie istnieje, ale .attr() robi podobne:

Ustawia atrybut disabled.

$("input").attr('disabled','disabled');

Aby włączyć ponownie, właściwą metodą jest użycie .removeAttr()

$("input").removeAttr('disabled');

W dowolnej wersji jQuery

Zawsze można polegać na rzeczywistym obiekcie DOM i jest prawdopodobnie trochę szybszy niż pozostałe dwie opcje, Jeśli masz do czynienia tylko z jednym elementem:

// assuming an event handler thus 'this'
this.disabled = true;

Zaletą użycia metod .prop() lub .attr() jest możliwość ustawienia właściwości dla kilku wybranych elementów.


Uwaga: w 1.6 jest .removeProp() metoda, która brzmi podobnie do removeAttr(), ale nie powinna być używana na natywnych właściwościach, takich jak 'disabled' fragment dokumentacji:

Uwaga: nie używaj tej metody do usuwania natywnych właściwości takich zaznaczone, wyłączone lub zaznaczone. Spowoduje to całkowite usunięcie właściwości i po usunięciu nie można jej ponownie dodać do elementu. Użyj .prop() ustawia te właściwości na false.

W rzeczywistości wątpię, że istnieje wiele legalnych zastosowań tej metody, właściwości logiczne są wykonywane w taki sposób, że powinieneś ustawić je na false zamiast "usuwać" je jak ich odpowiedniki "atrybuty" w 1.5

 3359
Author: gnarf,
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-06-25 20:18:33

Tylko ze względu na nowe konwencje & & czyniąc go elastycznym w przyszłości (chyba że coś się drastycznie zmieni z ECMA6 (????):

$(document).on('event_name', '#your_id', function() {
    $(this).removeAttr('disabled');
});

I

$(document).off('event_name', '#your_id', function() {
    $(this).attr('disabled','disabled');   
});
 53
Author: geekbuntu,
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-05-29 12:43:32
    // Disable #x
    $( "#x" ).prop( "disabled", true );
    // Enable #x
    $( "#x" ).prop( "disabled", false );

Czasami musisz wyłączyć / włączyć element formularza, taki jak input lub textarea. Jquery pomaga łatwo to zrobić z ustawieniem atrybutu disabled na "disabled". Dla np.:

  //To disable 
  $('.someElement').attr('disabled', 'disabled');

Aby włączyć wyłączony element musisz usunąć atrybut "disabled" z tego elementu lub opróżnić jego łańcuch. Dla np.:

//To enable 
$('.someElement').removeAttr('disabled');

// OR you can set attr to "" 
$('.someElement').attr('disabled', '');

Zobacz: http://garmoncheg.blogspot.fr/2011/07/how-to-disableenable-element-with.html

 26
Author: Harini Sekar,
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-04-07 10:27:16
$("input")[0].disabled = true;

Lub

$("input")[0].disabled = false;
 12
Author: Sajjad Shirazy,
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-09-11 08:33:11

Możesz umieścić to gdzieś globalnie w swoim kodzie:

$.prototype.enable = function () {
    $.each(this, function (index, el) {
        $(el).removeAttr('disabled');
    });
}

$.prototype.disable = function () {
    $.each(this, function (index, el) {
        $(el).attr('disabled', 'disabled');
    });
}

A potem możesz pisać takie rzeczy jak:

$(".myInputs").enable();
$("#otherInput").disable();
 7
Author: Nicu Surdu,
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-10-26 23:35:01

Jeśli chcesz tylko odwrócić bieżący stan (jak zachowanie przycisku przełączania):

$("input").prop('disabled', ! $("input").prop('disabled') );
 5
Author: daVe,
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-09-12 16:28:25

Aktualizacja na rok 2018:

Teraz nie ma potrzeby jQuery i minęło trochę czasu od document.querySelector lub document.querySelectorAll (dla wielu elementów) wykonują prawie dokładnie to samo zadanie co$, plus bardziej wyraźne getElementById, getElementsByClassName, getElementsByTagName

Wyłączenie jednego pola klasy "input-checkbox"

document.querySelector('.input-checkbox').disabled = true;

Lub wiele elementów

document.querySelectorAll('.input-checkbox').forEach(el => el.disabled = true);
 2
Author: Pawel,
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-02-11 18:29:49

Możesz użyć metody jQuery prop (), aby wyłączyć lub włączyć element formularza lub kontrolować dynamicznie za pomocą jQuery. Metoda prop () wymaga jQuery 1.6 i nowszych.

Przykład:

<script type="text/javascript">
        $(document).ready(function(){
            $('form input[type="submit"]').prop("disabled", true);
            $(".agree").click(function(){
                if($(this).prop("checked") == true){
                    $('form input[type="submit"]').prop("disabled", false);
                }
                else if($(this).prop("checked") == false){
                    $('form input[type="submit"]').prop("disabled", true);
                }
            });
        });
    </script>
 2
Author: SPARTAN,
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-08-02 10:26:25

Istnieje kilka sposobów na włączenie / wyłączenie dowolnego elementu :

Podejście 1

$("#txtName").attr("disabled", true);

Podejście 2

$("#txtName").attr("disabled", "disabled");

Jeśli używasz jQuery w wersji 1.7 lub wyższej, użyj prop () zamiast attr ().

$("#txtName").prop("disabled", "disabled");

Jeśli chcesz włączyć dowolny element, musisz zrobić coś przeciwnego do tego, co zrobiłeś, aby go wyłączyć. Jednak jQuery zapewnia inny sposób na usunięcie dowolnego atrybutu.

Podejście 1

$("#txtName").attr("disabled", false);

Podejście 2

$("#txtName").attr("disabled", "");

Podejście 3

$("#txtName").removeAttr("disabled");

Ponownie, jeśli używasz jQuery w wersji 1.7 lub wyższej, użyj prop () zamiast attr (). Tak. W ten sposób włączasz lub wyłączasz dowolny element za pomocą jQuery.

 2
Author: wild coder,
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-08-02 10:59:38

Disable:

$('input').attr('readonly', true); // Disable it.
$('input').addClass('text-muted'); // Gray it out with bootstrap.

Enable:

$('input').attr('readonly', false); // Enable it.
$('input').removeClass('text-muted'); // Back to normal color with bootstrap.
 1
Author: Tomer Ben David,
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-21 07:04:29

Użyłem odpowiedzi @ gnarf i dodałem ją jako funkcję

   $.fn.disabled = function (isDisabled) {
     if (isDisabled) {
       this.attr('disabled', 'disabled');
     } else {
       this.removeAttr('disabled');
     }
   };

Następnie użyj w ten sposób

$('#myElement').disable(true);
 0
Author: Imants Volkovs,
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-11-16 10:24:59

2018, bez JQuery (ES6)

Wyłącz wszystkie input:

[...document.querySelectorAll('input')].map(e => e.disabled = true);

Wyłącz input z id="my-input"

document.getElementById('my-input').disabled = true;

pytanie brzmi z JQuery, tak dla twojej informacji.

 0
Author: rap-2-h,
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-06-04 13:52:45
<html>
<body>

Name: <input type="text" id="myText">



<button onclick="disable()">Disable Text field</button>
<button onclick="enable()">Enable Text field</button>

<script>
function disable() {
    document.getElementById("myText").disabled = true;
}
function enable() {
    document.getElementById("myText").disabled = false;
}
</script>

</body>
</html>
 -1
Author: ,
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
2015-09-26 10:22:38

W jQuery Mobile:

Dla wyłączenia

$('#someselectElement').selectmenu().selectmenu('disable').selectmenu('refresh', true);
$('#someTextElement').textinput().textinput('disable');

Dla enable

$('#someselectElement').selectmenu().selectmenu('enable').selectmenu('refresh', true);
$('#someTextElement').textinput('enable');
 -1
Author: Atif Hussain,
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-01-08 14:27:23

Wyłącz true dla typu wejścia:

W przypadku określonego typu wejścia ( Ex. Text type input )

$("input[type=text]").attr('disabled', true);

Dla wszystkich typów danych wejściowych

$("input").attr('disabled', true);
 -1
Author: Hasib Kamal,
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-22 07:02:50