javascript - 将 onclick 控件发送到 javascript 函数

标签 javascript function parameters attributes

我试图将属性及其值从 onclick 事件传递给 javascript 函数,但返回时未定义。

<script type="text/javascript">

function addParameters(control) {
         alert(control._userid);
        alert(control._flag);
}

<button text="Button" ID="btn1" _userid="datatest" _flag="datafromdatabaase" title="Add this to the list" onclick="addParameters(this);"> Button</button>

最佳答案

您不能像那样直接访问那些自定义属性。您需要通过元素的 attributes 属性访问它们。试试这个:

<button text="Button" ID="btn1" _userid="datatest" _flag="datafromdatabaase" title="Add this to the list" onclick="addParameters(this);"> Button</button>

<script type="text/javascript">

function addParameters(control) {
  alert(control.attributes._userid);
  alert(control.attributes._flag);
}
</script>

如果你想获取这些属性的值,试试这个:

<button text="Button" ID="btn1" _userid="datatest" _flag="datafromdatabaase" title="Add this to the list" onclick="addParameters(this);"> Button</button>

<script type="text/javascript">

function addParameters(control) {
  alert(control.attributes._userid.nodeValue);
  alert(control.attributes._flag.nodeValue);
}
</script>

关于javascript - 将 onclick 控件发送到 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33049450/

相关文章:

Javascript 读取 html 输入值

javascript - 在 VSCode 中禁用 Javascript 的特定错误消息?

javascript - 覆盖数据确认值

r - 为什么我的qqnorm图不能作为函数的一部分或括号内运行?

PHP call_user_func 与仅调用函数

function - 如何编写打开和关闭数据库连接的常用函数?

javascript - 检查是否所有复选框都被选中,然后将类添加到某物

mysql - 存储过程返回空值

PostgreSQL:dblink 查询中没有参数 $1

asp.net - 是否有支持请求参数连接的 URL 构建器?