第一张图为script界面的初始化,给i赋值0
第二张图为每次点击按钮会把i的值提交到count中
问题出在每次提交后,i都被赋值为0,找不到原因。(相当于每次submit后刷新界面flag无法得到request传入的值,导致每次给i赋值为0)
源码如下
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="dao.DBConnection"%>
<%@ page import="java.sql.*" %>
<%@ page import="entities.Patient" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page isELIgnored="false" %>
<%@ page import="dao.PatientDao" %>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="mainStyle.css">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body onload="init()">
<div id="content">
<div id="top"><a href="https://baike.baidu.com/item/%E5%8D%97%E6%98%8C%E8%88%AA%E7%A9%BA%E5%A4%A7%E5%AD%A6%E5%8C%BB%E9%99%A2/10389722?fr=aladdin"><img src="../picture/logo.png" alt="nchu"></a></div>
<div id="result_area">
<div id="result_head">
<table id="t_head">
<col width="40">
<col width="40">
<col width="40">
<col width="40">
<col width="40">
<thead>
<tr>
<th>序号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>状态</th>
</tr>
</thead>
</table>
</div>
<div id="result_data" onscroll="">
<table id="t_data" class="ta">
<col width="40">
<col width="40">
<col width="40">
<col width="40">
<col width="40">
</table>
</div>
</div>
<div id = "buttons">
<div class="inputBox">
<input type="button" class="button" onClick="last()" value="上一个">
</div>
<div class="inputBox2">
<input type="button" class="button" onClick="call()" value="呼叫">
</div>
<div class="inputBox3">
<input type="button" class="button" onClick="next()" value="下一个">
</div>
<div class="inputBox4">
<input type="button" class="button" onClick="finish()" value="完成">
</div>
</div>
</div>
<form name = "frmApp" action = "update" id= "frmAppId" method= "post" hidden>
<input id= "old" type= "text" name = "old" value="">
<input id= "ne_w" type= "text" name = "ne_w" value="">
<input id= "name" type= "text" name = "name" value="">
<input id= "count" type= "text" name = "count" value="">
</form>
<%
if((String)session.getAttribute("username")!=null&&!session.getAttribute("username").equals(""))
{
String sql="select Patient_name,Patient_birth,Patient_sex,Registered_status from Patient where Doctor_no = '"+(String)session.getAttribute("username")+"' and Registered_time = '2021-06-05 00:00:00.000'";
//String countSql = "select count(*) from Patient where Doctor_no = '"+(String)session.getAttribute("username")+"' and Registered_time = '2021-06-05 00:00:00.000'";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;database=Hospital_Database";
String user = "sa";//登录数据库时的用户名
String password = "sa";//登录数据库时的密码
Connection conn = DriverManager.getConnection(url,user,password);//数据源
Statement stat = conn.createStatement();
/* ResultSet rs1 = stat.executeQuery(countSql); //得到数量
int num = 0;
while(rs1.next())
{
num = Integer.parseInt(rs1.getString(1));
}
session.setAttribute("num", num);
Patient p[] = new Patient[num];
*/
ResultSet rs = stat.executeQuery(sql); //执行搜索
List<Patient> list = new ArrayList<Patient>(); //存信息
String allInf = ""; //所有信息
int i = 0; //用于遍历
while(rs.next()){
/* p[i].setPatient_name(rs.getString(1));
p[i].setPatient_birth(rs.getString(2));
p[i].setPatient_sex(rs.getString(3));
p[i].setRegistered_status(rs.getString(4));
list.add(i, p[i]);
i++; */
/* Patient pp = new Patient();
pp.setPatient_name(rs.getString(1));
pp.setPatient_birth(rs.getString(2));
pp.setPatient_sex(rs.getString(3));
pp.setRegistered_status(rs.getString(4));
list.add(pp); */
allInf+=rs.getString(1)+"&"+rs.getString(2)+"&"+rs.getString(3)+"&"+rs.getString(4)+"/";
}
request.setAttribute("patientInf", allInf);
}
out.print(request.getParameter("count"));
//request.setAttribute("count", request.getParameter("count"));
%>
<script>
var flag = "<%=request.getAttribute("count")%>";
if(flag=='null'){
flag = "0";
}
var i = Number(flag);
//alert(i);
function next() //下一个
{
var table = document.getElementById("t_data");
if(i<table.rows.length){
i++;
//table.rows[i].style.background = "gray";
//table.rows[i-1].style.background = "transparent";
//var vvvv = document.getElementById( "count" );
//alert(vvvv.value);
//alert(i);
}
else{
alert("没有下一个了!");
}
if(table.rows[i-1].cells[4].innerHTML=="呼叫中"||table.rows[i-1].cells[4].innerHTML=="排队中") //如果是上一个没有完成,直接改为过号状态
{
var old = table.rows[i-1].cells[4].innerHTML;
//table.rows[i-1].cells[4].innerHTML = "已过号";
//table.rows[i-1].cells[4].style.background = "red";
var ne_w = "已过号";
var name = table.rows[i-1].cells[1].innerHTML;
document.getElementById( "old" ).value = old; // 将JS变量值存储到隐藏控件中
document.getElementById( "ne_w" ).value = ne_w;
document.getElementById( "name" ).value = name;
document.getElementById( "count" ).value = i; //指针(用于刷新后标记灰色)
alert(document.getElementById( "count" ).value);
var frm = document.getElementById( "frmAppId" ); // 获取表单
//alert(document.getElementById( "count" ).value);
frm.submit(); // 对表单进行提交
}
}
function last() //上一个
{
var table = document.getElementById("t_data");
if(i>0){
// table.rows[i].style.background = "transparent";
// table.rows[i-1].style.background = "gray";
i--;
}
else{
alert("没有上一个了!");
}
document.getElementById( "count" ).value = i; //指针
var frm = document.getElementById( "frmAppId" ); // 获取表单
frm.submit(); // 对表单进行提交
}
function finish() //完成单号
{
var table = document.getElementById("t_data");
var old = table.rows[i].cells[4].innerHTML;
var name = table.rows[i].cells[1].innerHTML;
// table.rows[i].cells[4].innerHTML = "已完成";
// table.rows[i].cells[4].style.background = "green";
var ne_w = "已完成";
document.getElementById( "old" ).value = old; // 将JS变量值存储到隐藏控件中
document.getElementById( "ne_w" ).value = ne_w;
document.getElementById( "name" ).value = name;
document.getElementById( "count" ).value = i; //指针(用于刷新后标记灰色)
var frm = document.getElementById( "frmAppId" ); // 获取表单
frm.submit();
}
function call() //呼号
{
var table = document.getElementById("t_data");
if(table.rows[i].cells[4].innerHTML!="已完成"){
table.rows[i].cells[4].innerHTML = "呼叫中";
table.rows[i].cells[4].style.background = "yellow";
flag = i; //获取到当前呼叫的号数
}
else
window.alert("该病号已完成,无需再次呼叫");
}
function init()
{
var information = "<%=(String)request.getAttribute("patientInf")%>";
var cnt = "<%=request.getAttribute("count")%>";
if(cnt=='null')
cnt = "0";
//alert(cnt);
var x = 0; //用于计数
var record = ""; //一条记录
var name = "";
var birth = "";
var sex = "";
var status = "";
var year = 2021;
if(information!=null)
{
while(i<information.split("/").length-1)
{
record = information.split("/")[x];
name = record.split("&")[0];
birth = record.split("&")[1];
sex = record.split("&")[2];
status = record.split("&")[3];
var birthYear = birth.split("-")[0];
var age = year-birthYear;
x++;
if(status=="已过号")
{
var str = "<tr><th>"+x+"</th><th>"+name+"</th><th>"+age+"</th><th>"+sex+"</th><th style='background: red;''>"+status+"</th></tr>";
}
else if(status=="叫号中")
{
var str = "<tr><th>"+x+"</th><th>"+name+"</th><th>"+age+"</th><th>"+sex+"</th><th style='background: yellow;'>"+status+"</th></tr>";
}
else if(status=="已完成")
{
var str = "<tr><th>"+x+"</th><th>"+name+"</th><th>"+age+"</th><th>"+sex+"</th><th style='background: green;'>"+status+"</th></tr>";
}
else
var str = "<tr><th>"+x+"</th><th>"+name+"</th><th>"+age+"</th><th>"+sex+"</th><th>"+status+"</th></tr>";
document.getElementById("t_data").innerHTML +=str;
}
}
var username = "<%=(String)session.getAttribute("username")%>";
}
</script>
</body>
</html>