我使用 javascript 添加了一行。现在我希望通过 id 从输入框文本中选取新列的值。其中存在某种错误,我无法理解。
<html>
<head>
<script>
function addtosubtotal() {
var table = document.getElementById("myTable");
var row = table.insertRow(2);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = getElementById("cid");
cell2.innerHTML = document.getElementById("name");
}
</script>
</head>
<body>
<table id="myTable" >
<tr>
<td>S. No</td>
<td>Item Name</td>
<td>Subtotal</td>
</tr>
<tr>
<td><input type="text" id="cid" name="id" /> </td>
<td><button onclick="addtosubtotal()" /></td>
</tr>
</table>
</body>
</html>
最佳答案
您没有输入 ID name
:
<input type="text" id="name" name="name" />
所以你不会通过id获取它
如果您愿意,您还可以在 JavaScript 中组合这两个语句
<script>
function addtosubtotal() {
var table = document.getElementById("myTable");
var row = table.insertRow(2);
var cell1 = row.insertCell(0).innerHTML = document.getElementById("cid").value;
var cell2 = row.insertCell(1).innerHTML = document.getElementById("name").value;
}
</script>
关于javascript - 使用innerHTML和GetElementById在输入框中插入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28995687/