根据下拉列表中的选择禁用输入字段

发布于 2024-08-27 14:49:25 字数 2145 浏览 11 评论 0原文

我有一个下拉框和它下面的一些文本输入字段。根据用户从下拉菜单中选择的项目,我想禁用某些字段。我认为我无法正确定位输入字段,但我无法弄清楚问题是什么:

这是我到目前为止得到的脚本:

$(document).ready(function(){
var customfield = $('#customfields-tf-19-tf');
var customfield1 = $('#customfields-tf-20-tf');
var customfield2 = $('#customfields-tf-13-tf');


$(function() {
  var call_table = {
    'Condominium': function() {
     customfield.attr("disabled");     
     },
    'Co-Op': function() {
     customfield1.attr("disabled");
     },
    'Condop': function() {
     customfield2.attr("disabled");
     }
  };

  $('#customfields-s-18-s').change(function() {
    call_table[this.value]();
  });

});

});

以及我的表单的布局:

<td width="260" class="left">
<label for="customfields-s-18-s">Ownership (Required):</label>
</td>
<td class="right">
<select name="customfields-s-18-s" class="dropdown" id="customfields-s-18-s" size="" >
<option value="Condominium"> Condominium</option>
<option value="Co-Op"> Co-Op</option>
<option value="Condop"> Condop</option>
</select>
</td>
</tr> 



<tr>
<td width="260" class="left">


  <label for="customfields-tf-19-tf">Maintenance:</label>
</td>
<td class="right">
  <input type="text" title="Maintenance" class="textInput" name="customfields-tf-19-tf" id="customfields-tf-19-tf"  size="40"/>
</td>

</tr>


<tr id="newsletter_topics">
<td width="260" class="left">

  <label for="customfields-tf-20-tf">Taxes:</label>
</td>
<td class="right">
  <input type="text" title="Taxes" class="textInput" name="customfields-tf-20-tf" id="customfields-tf-20-tf"  size="40" />
</td>
</tr>


<tr>
<td width="260" class="left">

  <label for="customfields-tf-13-tf" class="required">Tax Deductibility:</label>
  </td>
<td class="right">
  <input type="text" title="Tax Deductibility" class="textInput" name="customfields-tf-13-tf" id="customfields-tf-13-tf" size="40" />
 </td>
 </tr>

I have a drop down box and some text input fields below it. Based on which item from the drop down menu the user selects, I would like to disable some of the fields. I think I am failing to target the input fields correctly but I can't figure out what the problem is:

Here is the script I have gotten so far:

$(document).ready(function(){
var customfield = $('#customfields-tf-19-tf');
var customfield1 = $('#customfields-tf-20-tf');
var customfield2 = $('#customfields-tf-13-tf');


$(function() {
  var call_table = {
    'Condominium': function() {
     customfield.attr("disabled");     
     },
    'Co-Op': function() {
     customfield1.attr("disabled");
     },
    'Condop': function() {
     customfield2.attr("disabled");
     }
  };

  $('#customfields-s-18-s').change(function() {
    call_table[this.value]();
  });

});

});

And the layout for my form:

<td width="260" class="left">
<label for="customfields-s-18-s">Ownership (Required):</label>
</td>
<td class="right">
<select name="customfields-s-18-s" class="dropdown" id="customfields-s-18-s" size="" >
<option value="Condominium"> Condominium</option>
<option value="Co-Op"> Co-Op</option>
<option value="Condop"> Condop</option>
</select>
</td>
</tr> 



<tr>
<td width="260" class="left">


  <label for="customfields-tf-19-tf">Maintenance:</label>
</td>
<td class="right">
  <input type="text" title="Maintenance" class="textInput" name="customfields-tf-19-tf" id="customfields-tf-19-tf"  size="40"/>
</td>

</tr>


<tr id="newsletter_topics">
<td width="260" class="left">

  <label for="customfields-tf-20-tf">Taxes:</label>
</td>
<td class="right">
  <input type="text" title="Taxes" class="textInput" name="customfields-tf-20-tf" id="customfields-tf-20-tf"  size="40" />
</td>
</tr>


<tr>
<td width="260" class="left">

  <label for="customfields-tf-13-tf" class="required">Tax Deductibility:</label>
  </td>
<td class="right">
  <input type="text" title="Tax Deductibility" class="textInput" name="customfields-tf-13-tf" id="customfields-tf-13-tf" size="40" />
 </td>
 </tr>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰雪梦之恋 2024-09-03 14:49:25

我相信,为了禁用输入,您需要使用:

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

此外,其中一些函数可能对您有用:

$.fn.enable = function() {
  return this.removeAttr('disabled');
}

$.fn.disable = function() {
  return this.attr('disabled', 'disabled');
}

$.fn.disenable = function(val) {
  if (val)
    return this.enable();
  else
    return this.disable()
}

$.fn.toggleEnabled = function() {
  if (this.attr('disabled') == 'disabled')
    return this.enable();
  else
    return this.disable();
}

一旦定义了它们,您就可以在任何 jQuery 变量上使用它们,如下所示:

$(input).toggleEnabled();

I believe that in order to disable an input you need to use:

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

Also, some of these functions might prove useful for you:

$.fn.enable = function() {
  return this.removeAttr('disabled');
}

$.fn.disable = function() {
  return this.attr('disabled', 'disabled');
}

$.fn.disenable = function(val) {
  if (val)
    return this.enable();
  else
    return this.disable()
}

$.fn.toggleEnabled = function() {
  if (this.attr('disabled') == 'disabled')
    return this.enable();
  else
    return this.disable();
}

once they've been defined, you can then use them on any jQuery variable, like so:

$(input).toggleEnabled();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文