javascript - 在 html 事件处理程序中调用 document.getElementsByName.Value

标签 javascript html onkeyup

只是想知道这是否是有效的语法。

<input type="text" name="memtype" value="1" onkeyup="javascript:gettype(document.getElementsByName('memtype').value);>

最佳答案

这将无效,因为 getElementsByName 是一个集合,需要传递索引,如

document.getElementsByName('memtype')[0]

function gettype(val) {
  console.log(val)
}
<input type="text" name="memtype" value="1" onkeyup="javascript:gettype(document.getElementsByName('memtype')[0].value);">

只需使用 this.value 传递值即可完成相同的操作

function gettype(val) {
  console.log(val)
}
<input type="text" name="memtype" value="1" onkeyup="gettype(this.value)">

关于javascript - 在 html 事件处理程序中调用 document.getElementsByName.Value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48604804/

相关文章:

javascript - 如何在 JavaScript 中用逗号打印一个数字作为千位分隔符

java - aui validator 无法处理在 liferay 中提交的表单

html - 仅获取特定 html 部分及其子部分的所有 css

javascript - 按 Enter 键时调用 onKeyup 事件函数不起作用

javascript - Jquery:拆分/连接字符串 onKeyup 事件

javascript - 如何对 IndexedDB 查询的结果进行排序?

javascript - If 语句中不显示对话框

javascript - 使用 Istanbul 尔的 nightwatch.js 的代码覆盖率

html - 如何删除焦点/悬停在选择/选项下拉菜单上的蓝色背景

javascript - 使用返回键关闭警报对话框将 keyup 事件发送到聚焦的输入框,导致无限循环