最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Disable dropdownlist based on the selected value of another - Stack Overflow

programmeradmin7浏览0评论

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 badges
Add a ment  | 

2 Answers 2

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")
发布评论

评论列表(0)

  1. 暂无评论