【发布时间】:2018-04-27 01:09:46
【问题描述】:
当使用特定值选择另一个下拉列表(我们称他为大师)时,我有几个下拉列表(我们称它们为子),每个下拉列表都显示为 javascript。 这个 javascript 工作得很好。 Now, I want that when a specific child has been set to a specific value, this child go back to its default value when another value of the master is selected.
到目前为止我的代码:
// On every 'Change' of the drop down with the ID "TailleSelector" call the displayTextField function
document.getElementById("TailleSelector").addEventListener("change", displayTextField);
function displayTextField() {
// Get the value of the selected drop down
var dropDownText = document.getElementById("TailleSelector").value;
// If selected text matches 'value', display the text field.
if (dropDownText == "1 à 2") {
document.getElementById("TailleBacGris12").style.display = 'block';
} else {
document.getElementById("TailleBacGris12").style.display = 'none';
document.getElementById("TailleBacGris12").value = '-';
}
}
最后一个 getElementById(在隐藏第一个子下拉列表之后)不起作用,当我更改主值时,该值未设置为“-”...
关于我如何完成这项工作的任何提示?
谢谢!
编辑
在按照评论中的要求检查 HTML 时,我发现了我的赌注……真可耻!
隐藏下拉列表的 ID 调用是包含选择的段落之一。给选择一个ID并在上面的js中调用它,在选择隐藏时授予正确的值。
我的坏人,非常感谢你的帮助。
【问题讨论】:
-
您能解释一下为什么您认为它不起作用吗?您在代码中或在调试过程中做了什么表明它不起作用?
-
默认值是否总是列表中的第一个?对于所有下拉菜单?
-
默认总是第一个,它的值是'-'。如果我测试我的下拉菜单,则使用选定的值隐藏子项,然后再次显示保留该值。
-
只是最后一条语句last getElementById(在隐藏第一个子下拉列表之后)不起作用你也可以添加你的html代码吗?
-
您正在将文本分配给该值,这不是您应该将选定值设置为下拉列表的方式。看看这个线程:stackoverflow.com/questions/3989324/…
标签: javascript dropdown