javascript 单击获取按钮值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16756048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:52:50  来源:igfitidea点击:

On click get button Value

javascript

提问by LynAs

I have a button like following

我有一个像下面这样的按钮

<input type='button' value='Generate' onclick='f1()' />

now the f1 function should show a alert box contain button value. in this case 'Generate'

现在 f1 函数应该显示一个包含按钮值的警告框。在这种情况下“生成”

How to do this?

这个怎么做?

I tried

我试过

alert(this);
alert(this.val());

it does not work

这是行不通的

回答by naveen

Try this.

试试这个。

<input type='button' value='Generate' onclick='f1(this)' />

Now alert like

现在警报像

function f1(objButton){  
    alert(objButton.value);
}

P.S: val()is actually a jQuery implementation of value

PS:val()实际上是一个jQuery实现value

回答by amaugo somto

Or you do this:

或者你这样做:

<button id='button' value='Generate' onclick='f1()'>Generate</button>

Then this for javascript:

然后这对于javascript:

Const click = document.getElementById('button')
Function f1{
  Alert(`${click.Value}`)
}