简体   繁体   English

如何将单击的按钮的值存储在javascript变量中?

[英]How to store the value of button clicked in a javascript variable?

I am trying to store the value of a button in a javascript variable once the button is clicked. 单击按钮后,我试图将按钮的值存储在javascript变量中。 Following is my button : 以下是我的按钮:

nameofService is also a javascript variable nameofService也是一个javascript变量

var a='<td><button type="button" className="btn btn-info" value="'+nameOfService+'" data-toggle="modal" data-target="#remove"><i class="glyphicon glyphicon-remove"></i>'
                      +'</button></td></tr>';

I want to print the button's value everytime it is clicked and pass on the value further. 我想每次单击按钮时打印按钮的值,然后进一步传递该值。

For dropdowns I was using the className="dropdown-button btn" because I was using bootstrap,anything like this possible? 对于下拉菜单,我使用的是className =“ dropdown-button btn”,因为我使用的是引导程序,是否可能这样?

Checkout this one 结帐这一个

 var nameofService = 'buttonValue'; var a ='<button type="button" id="modalButton" className="btn btn-info" value="' + nameofService + '" data-toggle="modal" data-target="#remove"><i class="glyphicon glyphicon-remove"></i></button>'; document.getElementById('myDiv').innerHTML = a; var buttonElement = document.getElementById('modalButton'); buttonElement.addEventListener("click", function(){ console.log(buttonElement.value); }); 
 <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <div id="myDiv"></div> 

I have created little stuff for you, it may definitely help to resolve your issue and you may dynamically get and set button value. 我为您创建了一些小东西,它肯定可以帮助您解决问题,并且可以动态获取和设置按钮值。 Please review snippet. 请查看摘要。

 var nameofService; var inc = 1; var content; var nameofService = "Button "+inc; function setUpValue(){ var a ='<td><input id="btn" type="button" className="btn btn-info" data-toggle="modal" data-target="#remove" value="'+nameofService+'" onClick="getNewValue()"></button></td>'; document.getElementById("div_").innerHTML = a; showNodeText(); }; function getNewValue(){ inc++; nameofService = "Button "+inc; document.getElementById("btn").value = nameofService; showNodeText(); }; function showNodeText(){ content = document.createTextNode(nameofService+"\\n"); document.getElementById("div1_").appendChild(content); } 
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Question 1</title> </head> <body onload="setUpValue()"> <div id="div_"></div> <div id="div1_"></div> </body> </body> </html> 

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

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