jQuery - Disable Form Fields

后端 未结 5 1558
礼貌的吻别
礼貌的吻别 2020-12-13 01:54

I have 2 fields: 1 input, 1 select box.

Is it possible to make the browser disable one of the fields when the user uses the other?

For example, if the user i

相关标签:
5条回答
  • 2020-12-13 02:19

    The jQuery docs say to use prop() for things like disabled, checked, etc. Also the more concise way is to use their selectors engine. So to disable all form elements in a div or form parent.

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

    And to enable again you could do

    $myForm.find(':input:disabled').prop('disabled',false);
    
    0 讨论(0)
  • 2020-12-13 02:19

    try this

    $("#inp").focus(function(){$("#sel").attr('disabled','true');});
    
    $("#inp").blur(function(){$("#sel").removeAttr('disabled');});
    

    vice versa for the select also.

    0 讨论(0)
  • 2020-12-13 02:22
    $('input, select').attr('disabled', 'disabled');
    
    0 讨论(0)
  • 2020-12-13 02:23

    Here's a jquery plugin to do the same: http://s.technabled.com/jquery-foggle

    0 讨论(0)
  • 2020-12-13 02:28
    <script type="text/javascript" src="jquery.js"></script>          
    <script type="text/javascript">
      $(document).ready(function() {
          $("#suburb").blur(function() {
              if ($(this).val() != '')
                  $("#post_code").attr("disabled", "disabled");
              else
                  $("#post_code").removeAttr("disabled");
          });
    
          $("#post_code").blur(function() {
              if ($(this).val() != '')
                  $("#suburb").attr("disabled", "disabled");
              else
                  $("#suburb").removeAttr("disabled");
          });
      });
    </script>
    

    You'll also need to add a value attribute to the first option under your select element:

    <option value=""></option>
    
    0 讨论(0)
提交回复
热议问题