使用 jquery 禁用特定条件的下拉列表
Disabling dropdown for a certain condition using jquery
>我有两个下拉列表,分别以"国家"和"州"命名。"国家/地区"下拉列表中有两个值,印度和巴基斯坦。如果我选择"印度",那么我的第二个下拉列表"国家"应该启用,但如果我选择"巴基斯坦",那么我的第二个下拉菜单应该被禁用。我想使用 jquery 来做到这一点。提前谢谢。
此问题可以分为以下几类:
If the country is changed, do the following:
Determine if the country is India. If it is, enable the state dropdown
or, if the country is not India, disable the state dropdown
用代码编写,它将是:
<select id="country">
<option value="india">India</option>
<option value="pakistan">Pakistan</option>
</select>
<select id="state">
<option value="1">State 1</option>
<option value="2">State 2</option>
<option value="3">State 2</option>
</select>
<script language="javascript">
$(document).ready(function() {
$("#country").change(function() { // The country value has been changed
if($(this).val() == 'india') { // The country is set to india
$("#state").prop('disabled', false); // Since the country is India, enable the state dropdown
} else { // The country is NOT India
$("#state").prop('disabled', true); // Since the country is NOT India, so disable the state dropdown
}
}
});
</script>
编写此代码的方法更"优雅"和"优化",但我认为对于刚刚学习谁来解决此类问题的人来说,以上是最清晰的。
$country.change(function(){
$state.prop('disabled', true)
if (/india/i.test($(this).val()))
$state.prop('disabled', false)
})
相关文章:
- 在Twitter BootStrap嵌套下拉列表中托管一个选择列表(IE 9特定)
- 下拉列表的多个条件
- 禁用特定下拉列表中的特定选项
- 填充从特定年份到当前年份的下拉列表
- 通过单击下拉列表中的特定值,在列表框中获取相应的值
- 我该如何在Rails中使下拉列表重定向到另一个包含特定信息的页面
- 将值设置为有条件禁用的下拉列表
- 使用 jquery 禁用特定条件的下拉列表
- 如果选择了下拉列表中的特定值,GAE 任务队列将不会继续
- Dojo 筛选选择 - 动态将下拉列表中的特定选项标记为禁用(灰显且不可选择)
- jQuery根据条件不显示下一个下拉列表
- 条件下拉列表
- 获取多级下拉列表,使其仅以特定的浏览器宽度显示
- 从 URL 获取选项值,以显示下拉列表中的特定菜单项
- 根据另一个下拉列表中的选定值显示特定的下拉列表
- 引导下拉列表永久显示 - 不特定于按钮单击
- 将具有特定类的文本添加到下拉列表中<选择>
- 根据选择访问基于下拉列表和特定文本框的字符串变量
- 根据下拉列表的条件禁用表单元素
- 如何重新加载具有从下拉列表中自动选择的特定selectedIndex的页面