javascript - html 包含许多类似的选择,这些选择在更改时会使用react

标签 javascript php html

我的 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/

相关文章:

html - 位置 :sticky div not sticky at the bottom of the screen

javascript - 无法通过 Media Source API 附加片段,得到“InvalidStateError : An attempt was made to use an object that is not, or is no longer, usable

javascript - 淡出悬停 div 上的其他 div

php - 网址编码 - 解码

php - 如何在php中的字符串中找到多个子字符串

javascript - 无法让 "Draggable"工作

html - 列数不在 Chrome 中的短列上拆分

javascript - 有没有办法在 AngularJS 中同时订阅多个事件?

javascript - 我的弹出窗口可以在 jsfiddle 中使用,但不能在我的网站上使用

php 查找div的内容