JSP页面中所需要的JavaScript事件及Ajax
1 <script type="text/javascript"> 2 function checkEmpNo(id){ 3 var empNoValue=document.getElementById(id).value; 4 $.ajax({ 5 type:‘post‘, 6 url:‘employee_checkEmpNoAndEmpName.htm‘, 7 data:‘empNo=‘+empNoValue, 8 success: function(result){ 9 if(result=="该用户名不存在!"){ 10 alert("该用户名不存在!"); 11 document.getElementById(id).value=""; 12 document.getElementById("empName").value=""; 13 document.getElementById(id).focus(); 14 }else{ 15 document.getElementById("empName").value=result; 16 document.getElementById("entranceStart").focus(); 17 } 18 }); 19 </script> 20 <form> 21 <table> 22 <tr> 23 <td><input id="empNo" onblur="checkEmpNo(this.id)" type="text" name="empNo" /></td> 24 <td><input id="empName" type="text" name="empName" /></td> 25 </tr> 26 </table> 27 </form>
后台Action所需的代码: public clasee empAction extends ActionSupport{ public String checkEmpNoAndEmpName() throws Exception { HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse respons = ServletActionContext.getResponse(); String empNo = request.getParameter("empNo");//获取页面提交给后台的Action的empNo respons.setContentType("text/html;charset=utf-8");//对响应的字符集进行设置 String sendStr = ""; List<EmployeeInfo> employeeInfoList = this.empService.checkEmpNo(empNo);//调用业务根据empNo层查询此用户 try { PrintWriter out=respons.getWriter();//多去打印对象 if (employeeInfoList.size()>0) { sendStr = employeeInfoList.get(0).getEmpName(); } else { sendString = "该用户名不存在!"; } out.print(sendStr);//将后台动态sendStr输出到JSP页面 out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } return null; } }
时间: 2024-11-04 20:12:43