我想在我的 jsp 页面中使用 javascript,以便将输入框的值插入表格行。
我的 Jsp 页面:-
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Title</title>
<script type="text/javascript">
function addData(){
var x = 1;
var a = document.getElementById('area').value;
var table = document.getElementByTagName('table')[0];
var row = table.insertRow(table.rows.length);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = x++;
cell2.innerHTML = a;
}
</script>
</head>
<body>
<h2>Area...</h2>
Area: <input type="text" id="area" name="area" required/><label>sq. ft.
<button onclick="addData();">Add To Table</button>
</br></br>
<div>
<h2>Area Table...</h2>
<table border="1">
<tr>
<th>Section</th>
<th>Area</th>
</tr>
<tr>
<td>1</td>
<td>125.4485</td>
</tr>
</table>
</div>
</body>
</html>
这里我想从输入框中向表格中插入一行。但是没有插入值。
代码有没有问题
最佳答案
使用浏览器的控制台开发者工具,查看错误,
这是错误:
Uncaught TypeError: document.getElementByTagName is not a function at addData (a.html:11) at HTMLButtonElement.onclick (a.html:28)
这意味着 javascript 无法识别此函数,因此您必须查看该函数的正确表示法
getElementsByTagName
关于Javascript 函数在 jsp 页面中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46681679/