单击按钮后,我试图将按钮的值存储在javascript变量中。以下是我的按钮:
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>';
我想在每次点击时打印按钮的值,并进一步传递该值。
对于下拉列表,我使用的是className =&#34;下拉按钮btn&#34;因为我使用的是bootstrap,这样的事情可能吗?
答案 0 :(得分:0)
结帐这个
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>
答案 1 :(得分:0)
我为你创建了一些小东西,它肯定有助于解决你的问题,你可以动态获取并设置按钮值。请查看代码段。
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>