【发布时间】:2016-08-31 13:44:35
【问题描述】:
我正在制作一个交互式表单。
用户将在两种 T 恤设计之间进行选择:“JS Puns”或“I love JS”。
T 恤共有 6 种颜色。如果选择了“JS Puns”,则只有前 3 种颜色可供选择。反之亦然,如果选择“我爱 JS”作为设计,则只会选择最后 3 种颜色。具体来说,颜色选择框中只会显示可用的颜色。
由于某种原因,我可以删除 1 或 2 种颜色,但无法删除所有必要的 3 种颜色。
这是我正在使用的 HTML 的 sn-p:
<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:
// T-Shirt Info section of the form.
// For the T-Shirt color menu, only display the options that match the design selected in the "Design" menu.
document.getElementById("design").addEventListener("change", function(){
var tShirtMenu = document.getElementById('design');
var tSelection = tShirtMenu.value;
var tColor = document.getElementById('color');
var colorSelection = tColor.options;
if(tSelection === "js puns"){
// If the user selects "Theme - JS Puns" then the color menu should only display "Cornflower Blue," "Dark Slate Grey," and "Gold."
colorSelection.remove(3);
colorSelection.remove(4);
//colorSelection.remove(5);
} else if (tSelection === "heart js"){
// If the user selects "Theme - I ♥ JS" then the color menu should only display "Tomato," "Steel Blue," and "Dim Grey."
colorSelection.remove(0);
colorSelection.remove(1);
colorSelection.remove(2);
}
});
有什么建议吗?谢谢
【问题讨论】:
-
如果你
.remove(0),你认为哪个元素在索引0? (另外,不是你要问的,但如果用户改变他们的选择呢?) -
请将解决方案留给未来的访客
标签: javascript dom drop-down-menu