使用单元格id为表中的单元格指定值

时间:2013-11-26 12:31:54

标签: java javascript html jsp web

我需要为jsp中的表格中的特定单元格指定一个值,并创建一个id为表格的表格':

现在使用以下命令为此表中的单元格指定值:

<%@ page import="java.io.*"%>
<html>
<head>
</head>
<body>
<table id="table1" border = "1">

<%
var table = document.getElementById("table1");
table.rows[1].cells[2].innerHTML = "UP";
%>

<tr>
<th></th>
<th>DEV1</th>
<th>DEV2</th>
<th>SIT1</th>
<th>SIT2</th>
<th>UAT</th>
<th>NFT</th>
</tr>

<tr>
<td>SFG Process</td>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
<td>row 1, cell 3</td>
<td>row 1, cell 4</td>
<td>row 1, cell 5</td>
<td>row 1, cell 6</td>
</tr>

<tr>
<td>SCC Process</td>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
<td>row 2, cell 3</td>
<td>row 2, cell 4</td>
<td>row 2, cell 5</td>
<td>row 2, cell 6</td>
</tr>

</table>
</body>
</html>

然而,Tomcat给出了错误:

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 9 in the jsp file: /emt/xyz.jsp
var cannot be resolved to a type
6: <table id="table1" border = "1">
7: 
8: <%
9: var table = document.getElementById("table1");
10: table.rows[1].cells[2].innerHTML = "UP";
11: %>
12: 


An error occurred at line: 9 in the jsp file: /emt/xyz.jsp
document cannot be resolved
6: <table id="table1" border = "1">
7: 
8: <%
9: var table = document.getElementById("table1");
10: table.rows[1].cells[2].innerHTML = "UP";
11: %>
12: 


Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:443)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:356)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:334)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:321)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:592)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)

2 个答案:

答案 0 :(得分:2)

<%
var table = document.getElementById("table1");
table.rows[1].cells[2].innerHTML = "UP";
%>

您正在jsp中编写java脚本代码,因此编译失败。

您只需删除该代码,然后在<script>代码

之间添加
<SCRIPT type="text/javascript">
  var table = document.getElementById("table1");
  table.rows[1].cells[2].innerHTML = "UP";
</SCRIPT>

See this link以获得完整参考和示例。

答案 1 :(得分:0)

尝试使用Javascript更改innerHTML!

var table = document.getElementById("table1");
table.rows[1].cells[2].innerHTML = "XYZ";