[英]How to get the selected value from dropdown by javaScript
I'm trying to code the javascript to change the javascript file src to get selected animation on background. 我正在尝试对javascript进行编码,以更改javascript文件src,以在背景上获取选定的动画。 Here's what I'm trying to do. 这就是我想要做的。 It would be appreciated if you would give me the right coding. 如果您能给我正确的编码,将不胜感激。 Thank you so much! 非常感谢! ^ - ^ ^-^
<select id="select" onchange="changeScript();">
<option value="js/style0.js" selected="selected">Basic</option>
<option value="js/style1.js">Spring</option>
<option value="js/style2.js">Summer</option>
<option value="js/style3.js">Autumn</option>
<option value="js/style4.js">Winter</option>
</select>
<script src="js/style.js" type="text/javascript" id="myscript"></script>
<script type="text/javascript"><!--
function changeScript(jsfile) {
document.getElementById('myscript').src = jsfile;
}
// --></script>
You can use the selectedIndexy
property to get the selected value from dropdown, something like 您可以使用selectedIndexy
属性从下拉列表中获取选定的值,例如
function changeScript() {
var select = document.getElementById("select"); //getting select element
var jsfile = select.options[select.selectedIndex].value;
//--------getting the selected value-------------^
document.getElementById('myscript').src = jsfile;
}
UPDATE 更新
You have defined a function changeScript(jsfile) but you are not passing any value. 您已经定义了一个函数changeScript(jsfile),但没有传递任何值。 It should at least be: 它至少应该是:
<select id="select" onchange="changeScript(this.value);">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.