我的任务是构建一个非常简单的应用程序,它在2行中有一系列下拉列表,当选择2时,一个简单的函数将2个值连接起来,并在它们旁边输出如下:dropdown1 dropdown2 Output
我想得到的是,一旦选择了第二个下拉值,函数就会运行,并在其显示output
的地方显示输出。但目前,似乎发生的是输出显示在一个新窗口中。
以下是我目前所掌握的(HTML):
<form>
<select id="test">
<option>Arena/Quantum Barcelona LTBC</option>
<option>Arena/Quantum Spain LTES</option>
</select>
<select id="name" onchange="tryThis()">
<option>Name</option>
<option>Name1</option>
</select>
</form>
JavaScript代码:
function tryThis() {
var string, string1 = '';
var e = document.getElementById("test");
string = e.options[e.selectedIndex].text;
var a = document.getElementById("name");
string1 = a.options[a.selectedIndex].text;
document.write(string+'_'+string1);
}
我是不是让事情变得比需要的更困难了?!
最佳答案
这是因为document.write
在显示某些内容之前会清除页面。你不应该使用这个函数。
相反,您可以将其附加到例如主体:
document.body.appendChild(
document.createTextNode(string + '_' + string2)
);
关于javascript - 触发功能后停止刷新页面,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8575212/