简体   繁体   English

变量作为JavaScript中的按钮值

[英]variable as button value in javascript

I will be reading a tag from xml and assign it to a variable, ID . 我将从xml中读取标签,并将其分配给变量ID

ID=(x[i].getElementsByTagName("ID-NUM")[0].childNodes[0].nodeValue);

How could I use the variable, ID , as the button value to display? 如何使用变量ID作为要显示的按钮值?

document.write("<input type = button value = ID style='width:100'><br><br>");

Kindly let me know if I an not clear, thanks. 请让我知道是否不清楚,谢谢。

您需要将该变量放入要写出的字符串中

document.write("<input type='button' value='" + ID + "' style='width:100%'/><br/><br/>");

另外,如果您已经写出了按钮对象,则可以直接使用对象模型:

document.getElementById("idOfButtonObject").value = ID;
document.write("<input type='button' value='" + ID + "' style='width:100;' />

Don't use document.write to insert anything on the page. 不要使用document.write在页面上插入任何内容。 It's problematic because if you do it after the object model has been constructed, it will wipe out the entire document and create a new one. 这是有问题的,因为如果在构造对象模型之后执行此操作,它将擦除整个文档并创建一个新文档。 Instead use the DOM methods to create a button. 而是使用DOM方法创建按钮。

var input = document.createElement('input');
input.type = 'button';
input.value = ID; // set the ID
input.style = 'width: 100';
document.body.appendChild(input); // add to page

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

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