javascript - 根据所选选项错误更改输入值 : is not a function

标签 javascript html

我正在尝试根据所选的更改输入元素 (url) 的值 选项(facetselection):

<script src="external.js" type="text/javascript"></script>
<script>
function facetselection(e) {
document.getElementById("ebscohosturl").value = e.target.value;
}
</script>

<input id="url" name="ebscohosturl" type="hidden" value="" />
<div id="facets">
  <select id="facetselection" onchange="facetselection(event)">
    <option id="nofacet" value="new_url" label="הכל">הכל</option>
    <option id="facetarticles" value="new_url2" label="מאמרים אקדמיים">מאמרים אקדמיים</option>
    <option id="facetbooks" value="new_url3" label="ספרים">ספרים</option>
    <option id="facetebooks" value="new_url4" label="ספרים אלקטרוניים">ספרים אלקטרוניים</option>
    <option id="facetthesis" value="new_url5" label="תזות">תזות</option>
    <option id="facetmaps" value="new_url6" label="מפות">מפות</option>
  </select>
</div>

这是基于找到的解决方案 here .我也试过this解决方案。 我收到一个错误:

Uncaught TypeError: facetselection is not a function
    at HTMLSelectElement.onchange

我错过了什么?我在脚本元素中定义了函数,我认为语法是正确的。

最佳答案

您的错误来自于您的函数与您的选择 ID 同名:facetselection。发生这种情况是因为您的选择是在一种形式中(它不在您的原始代码示例中)。

重命名您的函数或您的选择 ID,您应该没问题。

它实际上是 Why JS function name conflicts with element ID? 的副本 但我不能标记它。

具有可重现行为的代码段:

function facetselection(e) {
  document.getElementById("url").value = e.target.value;
}
<form>
<input id="url" name="ebscohosturl" type="text" value="" />
<div id="facets">
  <select id="facetselection" onchange="facetselection(event)">
    <option id="nofacet" value="new_url" label="הכל">הכל</option>
    <option id="facetarticles" value="new_url2" label="מאמרים אקדמיים">מאמרים אקדמיים</option>
    <option id="facetbooks" value="new_url3" label="ספרים">ספרים</option>
    <option id="facetebooks" value="new_url4" label="ספרים אלקטרוניים">ספרים אלקטרוניים</option>
    <option id="facetthesis" value="new_url5" label="תזות">תזות</option>
    <option id="facetmaps" value="new_url6" label="מפות">מפות</option>
  </select>
</div>
</form>

更正的片段

function facetSelection(e) {
  document.getElementById("url").value = e.target.value;
}
<form>
<input id="url" name="ebscohosturl" type="text" value="" />
<div id="facets">
  <select id="facetselection" onchange="facetSelection(event)">
    <option id="nofacet" value="new_url" label="הכל">הכל</option>
    <option id="facetarticles" value="new_url2" label="מאמרים אקדמיים">מאמרים אקדמיים</option>
    <option id="facetbooks" value="new_url3" label="ספרים">ספרים</option>
    <option id="facetebooks" value="new_url4" label="ספרים אלקטרוניים">ספרים אלקטרוניים</option>
    <option id="facetthesis" value="new_url5" label="תזות">תזות</option>
    <option id="facetmaps" value="new_url6" label="מפות">מפות</option>
  </select>
</div>
</form>

关于javascript - 根据所选选项错误更改输入值 : is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51295117/

相关文章:

html - 在 Flash 对象内滚动时防止网页滚动

javascript - 访问 JSON 子项时出现问题

javascript - 在引导模式中打印内容时未显示内容,但打印空白页

javascript - Chrome 扩展 - 从 list v2 迁移到 v3

javascript - 使用javascript重写输入值添加 "#"

javascript - 阻止用户输入英文并强制输入希伯来语

javascript - 检测是否从特定 URL 或域重定向

javascript - 如何使用 Javascript 动态填充 Django 模板中的选择列表?

html 视频不在 iphone 上加载但在桌面网络和 android 上加载

javascript - 结束播放功能不起作用的 HTML5 视频循环 src 更改