首页 > 解决方案 > 为什么单击一行时我的表格没有响应

问题描述

我创建了一个从数据库中获取值并将其显示在表中的 JSP。现在我想为表格创建一个点击功能。单击将显示一些值的行。我为此使用了一个 js 代码,但是当我单击该行时,什么也没发生。任何人都可以帮忙吗?谢谢。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="SCOfetch.*" %>
<%@ page import="java.util.ArrayList" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>First Page</title>
<% JCOtest connection1 = new JCOtest(); %>
    <%
        ArrayList<CompanyRecord> list = new ArrayList<CompanyRecord>();
        list = connection1.step4QueryTable();
    %>
//*This is the click function*
<script language="javascript">

function showBgc(idn){
alert (idn);
}

</script>    
</head>
<body>
    <c:set var="greeting" value="Hello, World!"/>

<!--    <img id="image-1" alt="" src="img/snow.jpg" width="300" height="300"/> -->
//This is the result table 
<table>
    <%
    int size=list.size();
    for(int i=0;i<size;i++){  
        CompanyRecord news =(CompanyRecord)list.get(i);  
        %>
        <tr>
        <td onclick="showBgc(i)"><%=news.getValue("Code") %></td>
        <td onclick="showBgc(i)"><%=news.getValue("Name") %></td>
        </tr><%
    }               
    %>              
</table>

</body>
</html> 

我在 IE 中调试它。html就像

 <tr>
     <td onclick="showBgc(i)">DE01</td>
     <td onclick="showBgc(i)">Country Template DE</td>
 </tr>

我没有定义错误。

标签: javascriptjsp

解决方案


您必须ishowBgc function call. 更改showBgc(i)showBgc(<%=i%>),否则该函数将i作为字符串值。

例子:

%>
    <tr>
        <td onclick="showBgc(<%=i%>)"><%=news.getValue("Code") %></td>
        <td onclick="showBgc(<%=i%>)"><%=news.getValue("Name") %></td>
    </tr> 
<%

显示codeName

<tr>
  <td onclick="showBgc('<%=news.getValue("Code") %>')"><%=news.getValue("Code") %></td>
  <td onclick="showBgc('<%=news.getValue("Name") %>')"><%=news.getValue("Name") %></td>
</tr>

推荐阅读