现在的位置: 首页 > 综合 > 正文

js实现img src更新请求

2014年03月08日 ⁄ 综合 ⁄ 共 4019字 ⁄ 字号 评论关闭
[javascript] view
plain
copy

  1. 1.javascript(更新的函数)  
[javascript] view
plain
copy

  1.    
[javascript] view
plain
copy

  1. <script type="text/javascript">  
  2. function changeImage(){  
  3.     var img = document.getElementById("imgVcode");  
  4.     if(img.name == 1){  
  5.         img.name = 2;  
  6.         img.src = "shop.do?method=registerImage2";  
  7.     }else if(img.name == 2){  
  8.         img.name = 1;  
  9.         img.src = "shop.do?method=registerImage";  
  10.     }  
  11. }  
  12. </script>  


 2.jsp中img和img改变的代码

[plain] view
plain
copy

  1. <td>  
  2.                             <img class="yzm_img" id='imgVcode' name="1" src="shop.do?method=registerImage" />  
  3.                             <input name="code" type="text" id="txtVerifyCode"  
  4.                                 class="yzm_input" onblur="testCode(this)"/>  
  5.                             <div class="text_left t1">  
  6.                                 <p class="t1">  
  7.                                     <span id="vcodeValidMsg">请输入图片中的四个字母。</span>  
  8.                                       
  9.                                     <span id="codeInfo" style="color:red"></span>  
  10.                                     <a href="#" id="imgchange" onclick="changeImage()">看不清楚?换个图片</a>  
  11.                                 </p>  
  12.                             </div>  
  13.                         </td>  


3.后台action代码

[java] view
plain
copy

  1. public ActionForward registerImage(ActionMapping mapping, ActionForm form,  
  2.             HttpServletRequest request, HttpServletResponse response)  
  3.             throws Exception {  
  4.         //将image创建,返回认证码  
  5.         response.setContentType("image/jpeg");  
  6.         OutputStream out = response.getOutputStream();  
  7.         String strEnsure = shopManager.createImageInfo(50,20,out);  
  8.           
  9.         //设置到session中  
  10.         request.getSession().setAttribute("strEnsure", strEnsure);  
  11.         return null;  
  12.     }  
  13.     public ActionForward registerImage2(ActionMapping mapping, ActionForm form,  
  14.             HttpServletRequest request, HttpServletResponse response)  
  15.             throws Exception {  
  16.         //将image创建,返回认证码  
  17.         response.setContentType("image/jpeg");  
  18.         OutputStream out = response.getOutputStream();  
  19.         String strEnsure = shopManager.createImageInfo(50,20,out);  
  20.           
  21.         //设置到session中  
  22.         request.getSession().setAttribute("strEnsure", strEnsure);  
  23.         return null;  
  24.     }  
[javascript] view
plain
copy

  1. 1.javascript(更新的函数)  
[javascript] view
plain
copy

  1.    
[javascript] view
plain
copy

  1. <script type="text/javascript">  
  2. function changeImage(){  
  3.     var img = document.getElementById("imgVcode");  
  4.     if(img.name == 1){  
  5.         img.name = 2;  
  6.         img.src = "shop.do?method=registerImage2";  
  7.     }else if(img.name == 2){  
  8.         img.name = 1;  
  9.         img.src = "shop.do?method=registerImage";  
  10.     }  
  11. }  
  12. </script>  


 2.jsp中img和img改变的代码

[plain] view
plain
copy

  1. <td>  
  2.                             <img class="yzm_img" id='imgVcode' name="1" src="shop.do?method=registerImage" />  
  3.                             <input name="code" type="text" id="txtVerifyCode"  
  4.                                 class="yzm_input" onblur="testCode(this)"/>  
  5.                             <div class="text_left t1">  
  6.                                 <p class="t1">  
  7.                                     <span id="vcodeValidMsg">请输入图片中的四个字母。</span>  
  8.                                       
  9.                                     <span id="codeInfo" style="color:red"></span>  
  10.                                     <a href="#" id="imgchange" onclick="changeImage()">看不清楚?换个图片</a>  
  11.                                 </p>  
  12.                             </div>  
  13.                         </td>  


3.后台action代码

[java] view
plain
copy

  1. public ActionForward registerImage(ActionMapping mapping, ActionForm form,  
  2.             HttpServletRequest request, HttpServletResponse response)  
  3.             throws Exception {  
  4.         //将image创建,返回认证码  
  5.         response.setContentType("image/jpeg");  
  6.         OutputStream out = response.getOutputStream();  
  7.         String strEnsure = shopManager.createImageInfo(50,20,out);  
  8.           
  9.         //设置到session中  

抱歉!评论已关闭.