admin管理员组文章数量:1379561
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. Here's my JS function:
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? Following it through in Firebug, it seems to fall into the correct statements, but ddlCust never gets disabled.
Any help would be most appreciated!
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. Here's my JS function:
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? Following it through in Firebug, it seems to fall into the correct statements, but ddlCust never gets disabled.
Any help would be most appreciated!
Share Improve this question asked Jun 19, 2009 at 10:31 RYFNRYFN 3,0891 gold badge31 silver badges41 bronze badges2 Answers
Reset to default 3==
instead of =
in your if:
if(ddlR.options[ddlR.selectedIndex].value == "3")
=
is for assignment, whereas ==
is for parison, in most C-like syntaxes.
You're not paring properly, you need to use '==':
if(ddlR.options[ddlR.selectedIndex].value == "3")
本文标签:
版权声明:本文标题:javascript - Disable dropdownlist based on the selected value of another - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744429008a2605846.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论