为什么我选择的所有选择器选项没有被删除?
Why aren't all my chosen selector options being removed?
我正在做一个交互式表单。
用户将在两种t恤设计中选择:"JS双关语"或"我爱JS"。
t恤共有6种颜色。如果选择了"JS双关语",那么只有前3种颜色可以选择。反之亦然,如果"我爱JS"被选为设计,那么只会选择最后3种颜色。具体来说,只有可用的颜色才会在颜色选择框中显示。
由于某种原因,我可以删除1或2种颜色,但我无法删除所有必要的3种颜色。
下面是我正在使用的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:
// 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);
}
});
有什么建议吗?谢谢你
好吧,这真的很糟糕…但是我像这样修改了它:
我删除了HTML中的所有选项,但添加了一个额外的:<option><-- Please select a T-shirt theme</option>
在我的JS中,然后我根据选择的主题选项重置颜色选择字段的innerHTML。
看起来是这样的。否则,正如nnnnnn所暗示的那样,如果我删除它们,我必须以某种方式将它们取出并将它们存储为临时变量,以便以后可以再次输入它们。
就是这样。
请让我知道我该如何改进:
// 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 === "selectTheme") {
tColor.innerHTML = "<option><-- Please select a T-shirt theme</option>";
} else 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."
tColor.innerHTML = "<option value='cornflowerblue'>Cornflower Blue</option><option value='darkslategrey'>Dark Slate Grey</option><option value='gold'>Gold</option>";
} 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."
tColor.innerHTML = "<option value='tomato'>Tomato</option><option value='steelblue'>Steel Blue</option><option value='dimgrey'>Dim Grey</option>";
}
});
相关文章:
- 带有预览和删除选项的图像上传-Javascript/Jquery
- 从dojo comboBox中删除选项
- 在 IE 中从 DOM 中删除选项时,滚动选择元素会跳到顶部
- 如何从选择 jsp 上的映射中删除选项
- 如何使用fine Uploader通过删除选项将上传的文件名传递到服务器端
- 如果选择框已在其他框中选中,则从该框中删除选项
- 使用jQuery的预览和删除选项上传多个图像
- 从选项卡面板中删除选项卡
- 从“按值选择”中删除选项
- jquery检查字符串是否已从文本区域删除,并从select中删除选项
- 修改googlechrome扩展以删除选项卡
- Jquery,从下拉列表中删除选项不起作用
- HTML选择框,通过javascript删除选项'无'选项出现
- 要添加的搜索功能&从select下拉列表中删除选项
- Javascript-动态网格花式删除选项
- 基于下拉选择从从属下拉列表中删除选项
- 上传文件与大小和删除选项在Javascript
- 两个选择-删除选项的一个基于另一个
- 通过焦点切换在TabPanel中添加和删除选项卡
- 删除选项绑定:如果选项被选中,如何从弹出框中动态删除选项