1
我是新来的ajax。 我正在使用ajax和jsp从数据库中检索数据。 当新数据插入数据库时,新数据将自动显示在Jsp页面中。因此在ajax代码中,我设置了Timeout()
。 在jsp页面中针对每个数据行都有一个复选框。如果我选中复选框,当页面重新加载时,复选框会被更改。但我想,当重新加载页面不变的复选框...如何保持“记住”复选框选择使用ajax页面重新加载
<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>
我假设你正在使用servlet;然后将选择值保存在**请求集属性**上,并在返回页面时重新分配它,例如'request.setAttribute(“attributeName”,attributeValue);'[在Servlet请求中设置属性](http:// www .devx.com/tips/Tip/15433),并通过表达式语言直接在jsp或javascript操作中检索它 – shareef