简体   繁体   English

在单击按钮时获取按钮的值,javascript

[英]Getting the value of a button when the button is clicked, javascript

I have buttons that I make with an each sentence in jade 我用玉中的每个句子制作按钮

each u in requests
    form.form-horizontal(method="post")
        input(type='button', value='Accept #{u.friend}', onclick='doAction(this.value)')
        input(type='button', value='Ignore #{u.friend}', onclick='doAction(this.value)')

When any button is pressed I want to know the value of the button in my js so I made this function 当按下任何按钮时,我想知道我的js中按钮的值,所以我做了这个功能

function doAction(value){
    console.log(value)
}

but when i press the button i always get that doAction is undefined, is there a way to define doAction before I press the button or get the value of the button in another way 但是当我按下按钮时,我总是得到doAction是未定义的,有没有一种方法可以在我按下按钮或以另一种方式获取按钮的值之前定义doAction

By doing : 通过做 :

each u in requests
    form.form-horizontal(method="post")
        input(type='button', value='Accept #{u.friend}', onclick='doAction(this)')
        input(type='button', value='Ignore #{u.friend}', onclick='doAction(this)')

and in you js : 在你的js中:

function doAction(obj){
    console.log(obj.value)
}

Here is a DEMO to get the value of button with JS 这是一个使用JS获取按钮值的演示

HTML HTML

<input type="button" value="1" onclick="getValue(this.value)">

JS JS

function getValue(value){
    alert(value)
    }

It should work, maybe you're referecing the wrong js file in your document 它应该可以工作,也许您在文档中引用了错误的js文件

each val in ["item1", "item2", "item3", "item4"]
    form.form-horizontal(method="post")
    input(type='button', value='Accept #{val}', onclick='doAction(this.value)')
    input(type='button', value='Ignore #{val}', onclick='doAction(this.value)')

JS: JS:

function doAction(value){
    alert(value);
}

demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM