溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

JQuery最的使用1

發(fā)布時間:2020-09-26 09:34:25 來源:網(wǎng)絡(luò) 閱讀:491 作者:xiaoqiang_sea 欄目:web開發(fā)

 1.查找xml中的值

 

  1. function tt(){ 
  2.     //var a = $("<xml><root><item>1111</item></root></xml>").find("item").length; 
  3.     var xml =   "<xml><root><ccc><item1>1111</item1><item2>2222</item2></ccc></root></xml>"
  4.     var ccc = $(xml).find("ccc").text();     
  5.     var item1 = $(xml).find("item1").text();     
  6.     var item2 = $(xml).find("item2").text();     
  7.     alert('ccc:'+ccc); 
  8.     alert('item1:'+item1); 
  9.     alert('item2:'+item2); 

 

2.用來驗(yàn)證用戶名是否存在

 

  1.     function verify(){ 
  2.         var jqueryObj = $("#username"); 
  3.         //獲取節(jié)點(diǎn)的值 
  4.         var userName = jqueryObj.val(); 
  5.          
  6.         alert(userName);  
  7.          
  8.         $.get("TestAction!test.action?name=" + userName,null,callback); 
  9.     } 
  10.     function check(){ 
  11.         //獲取節(jié)點(diǎn)的值 
  12.         var userName = $("#username").val(); 
  13.         if(userName=="" || userName=="請輸入用戶名"){ 
  14.             alert("用戶名不能為空"); 
  15.         }else{ 
  16.             $.get("TestAction!xml.action?name=" + userName,null,callback); 
  17.         } 
  18.          
  19.     } 
  20. function callback(data){ 
  21.     //alert("服務(wù)器返回值:"+data); 
  22.     var resultObj = $("#result"); 
  23. //  var domObj = xmlhttp.responseXML; 
  24.     var domObj = data
  25.     //<message>xxxx</message> 
  26.     //dom中利用getElementsByTagName可以根據(jù)標(biāo)簽名獲取元素節(jié)點(diǎn) 
  27.     //var messageNodes = domObj.getElementsByTagName("message"); 
  28.     //message標(biāo)簽中的文本在dom中是message標(biāo)簽所對應(yīng)的元素節(jié)點(diǎn)的子節(jié)點(diǎn),firstChild可以獲得當(dāng)前節(jié)點(diǎn)的第一個子節(jié)點(diǎn) 
  29.     //var textNode = messageNodes[0].firstChirld; 
  30.     //通過nodeVale可以獲取節(jié)點(diǎn)的值 
  31.     //var responseMessage = textNode.nodeVale; 
  32.     var responseMessage = $(data).find("message").text(); 
  33.     /*$(xml).find("message").each(function() { 
  34.         var field = $(this); 
  35.         //var fName = field.attr("Name");//讀取節(jié)點(diǎn)屬性 
  36.         //var dataType = field.find("datatype").text();//讀取子節(jié)點(diǎn)的值 
  37.         alert(field.text()); 
  38.     });*/    
  39.     resultObj.html(responseMessage); 
  40.      
  41. }      
  42.  
  43. function postsubmit(){ 
  44.         $.post("TestAction!test.action?name=" + $("#username").val(),null,callback); 
  45.  

 

  1. <input id="username" name="username" value="請輸入用戶名"></input> 
  2. <input type="button" onclick="verify()" value="確定"/> 
  3. <input type="button" onclick="check()" value="驗(yàn)證用戶名是否存在"/> 

服務(wù)端代碼:

 

  1. public void test() throws Exception { 
  2.     // TODO Auto-generated method stub 
  3.      System.out.println("name:"+new String(name.getBytes("iso-8859-1"),"utf-8")); 
  4.      HttpServletResponse httpServletResponse = ServletActionContext.getResponse(); 
  5.      httpServletResponse.setContentType("text/html;charset=utf-8"); 
  6.      PrintWriter pw =  httpServletResponse.getWriter(); 
  7.      pw.print("可以注冊"); 

 3.$.ajax的簡單使用

 

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 
  2. <
  3. String path = request.getContextPath(); 
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
  5. %> 
  6.  
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
  8. <html> 
  9.   <head> 
  10.     <base href="<%=basePath%>"> 
  11.      
  12.     <title>My JSP 'test2.jsp' starting page</title> 
  13.      
  14.     <meta http-equiv="pragma" content="no-cache"> 
  15.     <meta http-equiv="cache-control" content="no-cache"> 
  16.     <meta http-equiv="expires" content="0">     
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
  18.     <meta http-equiv="description" content="This is my page"> 
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     --> 
  22. <script type="text/javascript" src="js/jquery-1.4.4.min.js"></script> 
  23. <script type="text/javascript"> 
  24. function t1(){ 
  25.     var jqueryObj = $("#username"); 
  26.     var userName = jqueryObj.val(); 
  27.     var obj = {name:"123",age:20}; 
  28.     //alert(userName); 
  29.     $.ajax({ 
  30.         type:"POST", 
  31.         url:"TestAction!xml.action", 
  32.         data:"name=" + userName, 
  33.         dataType : "xml", 
  34.         success:callback 
  35.      
  36.     }); 
  37.      
  38.     //回調(diào)函數(shù) 
  39.     function callback(data){ 
  40.     var jqueryObj = $(data); 
  41.     var message = jqueryObj.children(); 
  42.     //alert(message); 
  43.     var text = message.text(); 
  44.     var resultObj = $("#aaa"); 
  45.     //alert(data); 
  46.     resultObj.html(text); 
  47.     //alert('3 '); 
  48.     } 
  49.  
  50.  
  51. </script> 
  52.   </head> 
  53.    
  54.   <body> 
  55.     <input id="username" name="username" value="請輸入用戶名"></input> 
  56.     <input type="button" onclick="t1()" value="1"/> 
  57.     <div id="aaa"></div> 
  58.   </body> 
  59. </html> 

 

  1.     public void xml() throws Exception { 
  2.         // TODO Auto-generated method stub 
  3.         System.out.println("name" + name); 
  4. //      name = new String(name.getBytes("iso-8859-1"),"utf-8"); 
  5. //      System.out.println("name" + name); 
  6.         StringBuffer sb = new StringBuffer(); 
  7.         HttpServletResponse httpServletResponse = ServletActionContext.getResponse(); 
  8.         httpServletResponse.setContentType("text/xml;charset=utf-8"); 
  9.         PrintWriter out =  httpServletResponse.getWriter(); 
  10.         sb.append("<xml><message>"); 
  11.         if(name.equals("wangweiqiang")){ 
  12.             sb.append("用戶名["+name+"]已經(jīng)存在,請使用其他用戶名").append("</message></xml>"); 
  13.         }else
  14.             sb.append("用戶名["+name+"]沒有使用").append("</message></xml>"); 
  15.              
  16.         } 
  17.         System.out.println(sb.toString()); 
  18.         out.println(sb.toString()); 
  19.     } 

 

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI