Disable/enable an input with jQuery?

后端 未结 18 2646
礼貌的吻别
礼貌的吻别 2020-11-21 07:47
$input.disabled = true;

or

$input.disabled = \"disabled\";

Which is the standard way? And, conversely, how do yo

相关标签:
18条回答
  • 2020-11-21 07:47
    $("input")[0].disabled = true;
    

    or

    $("input")[0].disabled = false;
    
    0 讨论(0)
  • 2020-11-21 07:47

    Approach 4 (this is extension of wild coder answer)

    txtName.disabled=1     // 0 for enable
    <input id="txtName">

    0 讨论(0)
  • 2020-11-21 07:47

    In jQuery Mobile:

    For disable

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

    For enable

    $('#someselectElement').selectmenu().selectmenu('enable').selectmenu('refresh', true);
    $('#someTextElement').textinput('enable');
    
    0 讨论(0)
  • 2020-11-21 07:50

    Update for 2018:

    Now there's no need for jQuery and it's been a while since document.querySelector or document.querySelectorAll (for multiple elements) do almost exactly same job as $, plus more explicit ones getElementById, getElementsByClassName, getElementsByTagName

    Disabling one field of "input-checkbox" class

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

    or multiple elements

    document.querySelectorAll('.input-checkbox').forEach(el => el.disabled = true);
    
    0 讨论(0)
  • 2020-11-21 07:50

    I used @gnarf answer and added it as function

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

    Then use like this

    $('#myElement').disable(true);
    
    0 讨论(0)
  • 2020-11-21 07:51

    You can use the jQuery prop() method to disable or enable form element or control dynamically using jQuery. The prop() method require jQuery 1.6 and above.

    Example:

    <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>
    
    0 讨论(0)
提交回复
热议问题