简体   繁体   English

根据另一个的选定值禁用下拉列表

[英]Disable dropdownlist based on the selected value of another

This may be a stupid question, but I'm still learning so do forgive me if the answer is obvious :) 这可能是一个愚蠢的问题,但是我仍然在学习,所以如果答案很明显,请原谅我:)

I have a test page with the following on: 我有一个测试页面,其中包含以下内容:

<select name="ddlRoles" ID="ddlRoles" OnChange="DisEnableDDL();">
<option value="-1">Fresh Milk</option>
<option value="2">Old Cheese</option>
<option value="3">Hot Bread</option>
</select>
<select name="ddlCust" ID="ddlCust">
<option value="-1">rawr</option>
<option value="2">root</option>
<option value="3">honk</option>
</select>

If the user selects anything with a value that's not 3 from ddl Roles, it should disable ddlCust. 如果用户从ddl Roles中选择值不为3的任何内容,则应禁用ddlCust。 Here's my JS function: 这是我的JS函数:

function DisEnableDDL()
{
var ddlR = document.getElementById("ddlRoles")
var ddlC = document.getElementById("ddlCust")
if(ddlR.options[ddlR.selectedIndex].value = "3")
 {
    ddlC.disabled = false;
 }
else
 {
    ddlC.selectedIndex = 0;
    ddlC.disabled = true;    
 }
}

Doesn't work. 不起作用 What am I missing? 我想念什么? have I failed to set my variables properly? 我无法正确设置变量吗? Does this just change the variables ddlR and ddlC without changing the elements on the page itself or something? 这是否只是更改了变量ddlR和ddlC而没有更改页面本身或其他元素? Following it through in Firebug, it seems to fall into the correct statements, but ddlCust never gets disabled. 在Firebug中遵循它,它似乎属于正确的语句,但是ddlCust从未被禁用。

Any help would be most appreciated! 非常感激任何的帮助!

== instead of = in your if: 如果您的情况是,则==代替=

if(ddlR.options[ddlR.selectedIndex].value == "3")

= is for assignment, whereas == is for comparison, in most C-like syntaxes. 在大多数类似C的语法中, =表示赋值,而==表示比较。

您的比较不正确,需要使用'==':

if(ddlR.options[ddlR.selectedIndex].value == "3")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据另一个下拉列表的选定值绑定下拉列表 - Bind dropdownlist based on the selected value of another dropdownlist 基于对另一个ASP:Dropdownlist.value的选择,禁用ASP:dropdownlist - Disable ASP:dropdownlist based on the selection of another ASP:Dropdownlist.value 根据从另一个dropdownList中选择的VALUE显示特定的dropdownList - Show a specific dropdownList based on selected VALUE from another dropdownList 根据从另一个 DropDownList 中选择的值填充 DropDownList - Fill a DropDownList based on the selected value from another DropDownList 根据在网格视图内另一个下拉列表上选择的值禁用网格视图内的下拉列表 - Disabling dropdownlist inside a gridview based on value selected on another dropdownlist inside the gridview 如何基于使用ajax从另一个下拉列表中选择的值来更改下拉列表中的选项? - How do I change the options from a dropdownlist based on the selected value from another dropdownlist using ajax? 如何根据另一个下拉列表的选定值填充下拉列表? MVC - How can I populate a dropdownlist based on the selected value of another dropdownlist? MVC 根据另一个选择中选择的值禁用或启用选择选项 - Disable or Enable select Options based on value selected in another select jQuery - 基于另一个字段选定值禁用输入字段 - jQuery - disable input field based on another field selected value 如何基于下拉列表选择的值创建图像? - How to create image based on dropdownlist selected value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM