我有两个选择标签,我希望当用户在每个选择标签中选择一个值时,输出显示这两个值:
例如,如果在第一个标签中有人选择 cars
,而在第二个标签中选择 golf
,
我想在输出中包含表达式 cars,golf
..我该怎么做?请帮助我
最佳答案
这是一个有效的 demo在纯 JavaScript 中。
我提出的解决方案是捕获 onchange
事件并使用 innerHTML
将其显示在 div 标记中。
HTML:
<select id="select1">
<option> cars </option>
<option> bikes </option>
</select>
<div id="output"></div>
JavaScript:
document.getElementById("select1").onchange=function(){
var e = document.getElementById("select1");
selected1 = e.options[e.selectedIndex].text;
if(selected2!="")
document.getElementById("output").innerHTML = selected1 + ", " + selected2;
};
关于javascript - 将两个 select 标签的值放在 <output>, html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22915253/