【发布时间】:2020-07-25 15:57:15
【问题描述】:
我有一个主下拉菜单,当在下拉菜单中选择一个选项时,在下一个文本字段中,另一个下拉菜单必须带有不同的值,并且当在文本中的主下拉菜单中选择其他选项时,必须出现在同一文本字段中。所以基本上我的问题是,当在下拉列表中选择选项时,如何在同一个输入字段中添加文本和 dropdwon。
I have tried to do the one part of the problem i.e , when first two option is selected the value is appearing in input field , but I'm not able to make the dropdown appear in the input field when third option is selected .
谁能帮我解决这个问题!
<html>
<body>
<label> Assets:</label>
<select id="name" onchange="func()">
<option value=""></option>
<option value="year">Calendar</option>
<option value="the current month">Month</option>
<option id="no-of-days">Days</option>
</select><br><br>
<label>Days:</label>
<input type="text" id="days">
<script>
function func() {
var dropdown = document.getElementById("name");
var selection = dropdown.value;
console.log(selection);
var TextBox = document.getElementById("days");
TextBox.value = selection;
document.getElementById('no-of-days').onclick = function() {
var values = ["1", "2", "3", "4"];
var select = document.createElement("select");
select.name = "date";
select.id = "date"
for (const val of values) {
var option = document.createElement("option");
option.value = val;
option.text = val.charAt(0).toUpperCase() + val.slice(1);
select.appendChild(option);
}
var label = document.createElement("label");
label.innerHTML = "Choose your date: "
label.htmlFor = "date";
document.getElementById("container").appendChild(label).appendChild(select);
}
}
</script>
</body>
</html>
【问题讨论】:
-
很抱歉我还不能测试它,因为我必须在 3 分钟内离开。但我认为您不会在 option 元素上获得点击事件。为什么不在选择上使用更改事件并在新选择更改为 3 值时添加它?
-
如果您的问题是基于从下拉列表中选择第三个选项来触发,那么您必须在下拉列表中调用
.change()并运行函数if ($(this).val() == 'option3')。如果您询问是否为文本和下拉菜单使用一个元素,请尝试<details>。检查此链接 (w3schools.com/html/tryit.asp?filename=tryhtml_elem_datalist)
标签: javascript html