jQuery - 禁用表单域 [英] jQuery - Disable Form Fields

查看:112
本文介绍了jQuery - 禁用表单域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个字段:1个输入,1个选择框。

当用户使用另一个字段时,是否可以使浏览器禁用其中一个字段? / p>

例如,如果用户正在将数据输入到输入中,则选择框将被禁用。如果用户正在从select(下拉菜单)中选择数据,则输入被禁用。



任何帮助都将非常感谢。



提前喝彩

解决方案

 < script type = text / javascriptsrc =jquery.js>< / script> 
< script type =text / javascript> ($(this).val()!='')。
$(document).ready(function(){
$(#suburb ')
$(#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>

您还需要为您的select元素下的第一个选项添加一个值属性:

 < option value =>< / option> 


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 is entering data into the input, the select box is disabled. If the user is selecting data from the select (dropdown), the input is disabled.

Any help with this would be much appreciated.

Cheers in advance.

解决方案

<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>

这篇关于jQuery - 禁用表单域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆