简体   繁体   中英

why my table doesn't respond when click a row

I ceate a JSP which fetch value from database and displayed it in a table. Now I want to create a click function for the table. Click the row it will display some value. I use a js code for it but when I click the line nothing happened. Can anyone help? Thanks.

<%@ 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> 

And I debug it in IE. the html is like

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

and the error is i not defined.

You have to print the i value in the showBgc function call . Change showBgc(i) to showBgc(<%=i%>) , otherwise the function will take i as a string value.

Example:

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

To show code and Name .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM