我创建了一个带有表单的页面,您可以在其中输入名称。 当您提交姓名时,该姓名将存储在名称“bezeichnung”下。 现在我想使用输入“bezeichnung”作为按钮标签。并将其附加到页面正文中。
这是这样的形式:
<head>
<link rel="stylesheet" href="\User\stl.css"/>
</head>
<body>
<h1> Neue Station hinzufügen</h1>
<form id="stationenformular" name="stationenformular" action="indexAktualisierung.html" method="get">
<div>
<label for="bezeichnung">Bezeichung der neuen Station:</label>
<input type="text" id="bezeichnung" name="bezeichung" />
</div>
<br><br>
<div>
<label for="ipadresse"> IP Adresse des Raspberry Pi:</label>
<input type="text" id="ipadresse" name="ipadresse"/text>
</div>
<div>
<br>
<input type="submit" value="Abschicken"/>
</div>
</form>
</body>
</head>
这是另一个 html 脚本中的函数:
var x = document.getElementsByName("bezeichnung");
var btn = document.createElement("BUTTON"); // Create a <button> element
var t = document.createTextNode(x); // Create a text node
btn.appendChild(t); // Append the text to <button>
document.body.appendChild(btn);
最佳答案
首先,您的name
是bezeichung
,而不是bezeichnung
。
此外,getElementsByName
返回一个类似数组的对象
。您需要从元素中获取第一项;
var x = document.getElementsByName("bezeichnung")[0]; // Get the first
var btn = document.createElement("BUTTON"); // Create a <button> element
var t = document.createTextNode(x.value); // Create a text node with x's value
btn.appendChild(t); // Append the text to <button>
document.body.appendChild(btn);
参见示例。我默认添加了文本。
var x = document.getElementsByName("bezeichnung")[0];
var btn = document.createElement("BUTTON"); // Create a <button> element
var t = document.createTextNode(x.value); // Create a text node
btn.appendChild(t); // Append the text to <button>
document.body.appendChild(btn);
<body>
<h1> Neue Station hinzufügen</h1>
<form id="stationenformular" name="stationenformular" action="indexAktualisierung.html" method="get">
<div>
<label for="bezeichnung">Bezeichung der neuen Station:</label>
<input type="text" id="bezeichnung" name="bezeichnung" value="Test"/>
</div>
<br><br>
<div>
<label for="ipadresse"> IP Adresse des Raspberry Pi:</label>
<input type="text" id="ipadresse" name="ipadresse"/text>
</div>
<div>
<br>
<input type="submit" value="Abschicken"/>
</div>
</form>
</body>
关于javascript - 使用 html 中名称为 ="example"的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42806812/