我是ajax的新手。我正在使用 ajax 和 jsp 从数据库中检索数据。当新数据插入数据库时,新数据会自动显示在 Jsp 页面中。所以在 ajax 代码中我设置了Timeout()
. 在针对每个数据行的 jsp 页面中,都有一个复选框。如果我选中复选框,则重新加载页面时复选框会更改。但是我想在重新加载页面时保持复选框不变...
<html>
<body onload="Ajax()">
<script type="text/javascript">
function Ajax()
{
var xmlHttp;
try
{
xmlHttp=new XMLHttpRequest();// Firefox, Opera 8.0+, Safari
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("No AJAX!?");
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4 && xmlHttp.status==200)
{
document.getElementById('ReloadThis').innerHTML=xmlHttp.responseText;
setTimeout('Ajax()',10000);
}
}
xmlHttp.open("GET","ajaxCalled.jsp",true);
xmlHttp.send(null);
}
默认文本
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" import="dbbean.DBBean,java.sql.*" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
function checkboxCheck()
{
var checkbox1=document.getElementsById("checkbox0")
if(checkbox1.checked)
{
alert(checkbox1);
checkbox1.checked="true";
}
else
{
alert(checkbox1);
}
}
</script>
</head>
<body onLoad="checkboxCheck()">
<table border=1 cellspacing=0 cellpadding=10>
<tr>
<th>checkbox</th>
<%
DBBean db=new DBBean();
Connection con=null;
ResultSet rs=null;
int count=0;
String checkName="checkbox";
String checkbox1="";
try
{
con=db.getDBConnection();
rs=db.execSQL("select * from login_history");
%>
<th>user_id</th>
<th>ip_address</th>
<th>login_status</th>
</tr>
<%
while(rs.next())
{
checkbox1=checkName+count;
count++;
out.println(checkbox1);
%>
<tr>
<td><input type="checkbox" id="<%= checkbox1 %>" ></td>
<td><%=rs.getString(1) %></td>
<td><%=rs.getString(2) %></td>
<td><%=rs.getString(3) %></td>
</tr>
<%
/* out.println(rs.getString(1));
out.println(rs.getString(2));
out.println(rs.getString(3));
out.println("</br></br>");*/
}
%>
</table>
<%
rs.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
%>
<div id="disp" style="visibility:hidden">
</div>
</body>
</html>