我的 html 包含许多类似的选择,我认为我可以这样做
<form name="form">
<select name="site" size="1" onChange="formHandler()">
<option value="$site?i=$i&j=$2">ABC</option>
...
</select>
</form>
html header中包含的是
function formHandler() {
var URL = "";
URL = document.form.site.options[document.form.site.selectedIndex].value;
if ( (URL != "") && (URL != "0") ) {
window.location.href = URL;
}
}
但是我收到 TypeError: document.form.site is undefined。我认为这是因为表单及其选择都具有相同的名称。这是因为我只想在 header 中使用一个 formHandler()。
非常感谢您的帮助!
最佳答案
将元素作为参数传递给处理函数:
onchange="formHandler(this)"
然后在处理程序中:
function formHandler(el) {
var url = el.options[el.selectedIndex].value;
// and the rest...
}
关于javascript - html 包含许多类似的选择,这些选择在更改时会使用react,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53696152/