Ajax提交資料判斷員工編號是否存在,及自動填滿與員工編號所對應的員工姓名。

來源:互聯網
上載者:User

標籤:res   字元   servlet   employee   html   自動   頁面   back   class   

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; }}

 

Ajax提交資料判斷員工編號是否存在,及自動填滿與員工編號所對應的員工姓名。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.