javascript - 输入属性通过 onclick 事件不可见

标签 javascript radio-button pug

我在 jade/pug View 中有一个简单的单选按钮表单,如下所示:

form
  input(type='radio',id="0", group="group0", value="some string" my_prop="hello", onclick='radioClick(this)')

在我的 js 脚本中,我定义了 radioClick 函数,如下所示:

function radioClick(e){
  console.log(e)
  console.log(e.my_prop)
  console.log(e.group)
  console.log(e.type)
  console.log(e.id)
}

输出如下所示:

<input type="radio" my_prop="hello" group="0" id="0" value="some string" onclick="radioClick(this)">
undefined //<--- Why are these undefined?
undefined //<--- Why are these undefined?
radio
0

为什么属性 groupmy_prop 未定义?当触发 onclick 事件时如何访问它们?

最佳答案

您可以使用 getAttribute() 并将所需的属性放入字符串中。

function radioClick(e){
  console.log(e.getAttribute('my_prop'));
  console.log(e.getAttribute('group'));
  console.log(e.getAttribute('type'));
  console.log(e.getAttribute('id'));
}
<input type="radio" my_prop="hello" group="0" id="0" value="some string" onclick="radioClick(this)">

关于javascript - 输入属性通过 onclick 事件不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41580294/

相关文章:

javascript - CSS 和 Jquery : addClass not overwriting css

django-forms - 如何创建具有互斥单选按钮的 Django 表单?

node.js - jade循环无法访问node.js文件中的变量

html - 关于 pre 标签中多行字符串的 Jade 语法

javascript - 使用 webpack 捆绑 js 应用程序作为 WordPress 插件

javascript - 如何将 HTML Div 与线条连接起来?

javascript - JS - 撇号 - 不会被替换

c# - 设置 myButton.Enabled = true 会触发单选按钮的 CheckedChanged 处理程序

javascript - 根据单选按钮显示带有消息的警报

node.js - Pug/Jade 插值不适用于属性