【发布时间】:2016-06-05 14:44:30
【问题描述】:
我很菜鸟,我知道我的 javascript 代码很重而且不是最漂亮的,但是我正在在线学习课程,所以如果你能提供一个解决方案来增强我的逻辑或基于类似的逻辑真的很有帮助。
我正在做一个项目,并且在 html 中有这两个下拉列表 -
<div>
<label for="design">Design:</label>
<select id="design" name="user_design">
<option>Select Theme</option>
<option value="js puns">Theme - JS Puns</option>
<option value="heart js">Theme - I ♥ JS</option>
</select>
</div>
<div id="colors-js-puns" class="">
<label for="color">Color:</label>
<select id="color">
<option value="cornflowerblue">Cornflower Blue (JS Puns shirt only)</option>
<option value="darkslategrey">Dark Slate Grey (JS Puns shirt only)</option>
<option value="gold">Gold (JS Puns shirt only)</option>
<option value="tomato">Tomato (I ♥ JS shirt only)</option>
<option value="steelblue">Steel Blue (I ♥ JS shirt only)</option>
<option value="dimgrey">Dim Grey (I ♥ JS shirt only)</option>
</select>
</div>
当页面加载时,设计位于“选择主题”选项上,并且颜色不需要填充到第二个列表中,直到选择了主题 - JS 双关语或主题 I
我已经根据选择的设计仅显示某些颜色,但无法弄清楚如何在选择其中一种设计之前不填充颜色列表的位置,我尝试将其设置为 if tShirts.value = == "" (为空,因为选择一件衬衫没有值),然后在抓取到 tShirts 变量中的所有元素上显示 none。这里有什么简单的修复方法吗?
var tShirts = document.getElementById("design");
//get the color option values into a variable
var ShirtColors = document.getElementById("color");
//function for the value of the design selection to change the corresponding color selector menu.
tShirts.onchange = function() {
if (tShirts.value == "js puns") {
ShirtColors.selectedIndex = 0;
ShirtColors[3].style.display = "none";
ShirtColors[4].style.display = "none";
ShirtColors[5].style.display = "none";
ShirtColors[0].style.display = "initial";
ShirtColors[1].style.display = "initial";
ShirtColors[2].style.display = "initial";
} else if (tShirts.value == "heart js") {
ShirtColors.selectedIndex = 3;
ShirtColors[3].style.display = "initial";
ShirtColors[4].style.display = "initial";
ShirtColors[5].style.display = "initial";
ShirtColors[0].style.display = "none";
ShirtColors[1].style.display = "none";
ShirtColors[2].style.display = "none";
}
}
}
【问题讨论】:
标签: javascript jquery