[英]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.