2017-10-11 64 views
1

我想在我的jsp页面中使用一个javascript,以便将输入框的值插入到表格行中。Javascript函数不能在jsp页面工作

我的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> 

在这里,我想插入一行到从输入框的表。但价值没有被插入。

代码中是否有任何问题。

回答

0

错字

尝试这样,TagName是多个selector.you缺少s

var table = document.getElementsByTagName('table')[0]; 

,而不是

var table = document.getElementByTagName('table')[0]; 

function addData() { 
 
    var x = 1; 
 
    var a = document.getElementById('area').value; 
 

 
    var table = document.getElementsByTagName('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; 
 
}
<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>

1

使用控制台开发工具的浏览器,看到的错误,
这里是错误:

Uncaught TypeError: document.getElementByTagName is not a function at addData (a.html:11) at HTMLButtonElement.onclick (a.html:28)

这意味着JavaScript并没有认识到这一点的功能,所以你要看看,右这是

getElementsByTagName

0

请更正拼写getElementByTagName功能的符号

相关问题