如何将按钮值传递到我的onclick事件函数中? [英] How to pass the button value into my onclick event function?

查看:40
本文介绍了如何将按钮值传递到我的onclick事件函数中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<input type="button" value="mybutton1" onclick="dosomething()">test

点击按钮时会产生dosomething函数,如何将按钮 mybutton1 的值作为参数传递给dosomething函数?

The dosomething function evoked when to click the button,how can pass the value of the button mybutton1 into dosomething function as it's parameter ?

推荐答案

您可以使用 this.value 将值传递给函数,其中 this 指向按钮

You can pass the value to the function using this.value, where this points to the button

<input type="button" value="mybutton1" onclick="dosomething(this.value)">

然后在函数中访问该值

function dosomething(val){
  console.log(val);
}

这篇关于如何将按钮值传递到我的onclick事件函数中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆