对 JavaScript 仍然很陌生,所以如果这很明显,我很抱歉,但我无法在这里找到答案。
我试图在根据下拉选项的值单击提交按钮时重定向。
<html>
<body>
<h1>test</h1>
<select id="eventChoice" style="width:35vw;">
<option value="theatre">Theatre</option>
<option value="comedy">Comedy</option>
<option value="music">Music</option>
<option value="open mic">Open Mic</option>
</select>
<button id="eventSubmit">Submit</button>
<script type="text/javascript">
var submit = document.getElementById('eventSubmit');
var choice = document.getElementById('eventChoice');
submit.onClick function(){
if (choice.selectedIndex == "Comedy"){
location.href="http://google.com";
}
}
</script>
</body>
</html>
任何帮助都会很棒。奇怪的是,我可以在 jQuery 中做到这一点,但我正在努力学习尽可能多的 javascript。
@Tom Bird:存在语法错误。
您可以像这样修改它,它应该可以工作。
submit.onclick = function(){
// selectedIndex for comedy is 1, select option are zeroth-index based.
if (choice.selectedIndex == 1){
location.href="http://google.com";
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句