您好,登錄后才能下訂單哦!
1.查找xml中的值
- function tt(){
- //var a = $("<xml><root><item>1111</item></root></xml>").find("item").length;
- var xml = "<xml><root><ccc><item1>1111</item1><item2>2222</item2></ccc></root></xml>";
- var ccc = $(xml).find("ccc").text();
- var item1 = $(xml).find("item1").text();
- var item2 = $(xml).find("item2").text();
- alert('ccc:'+ccc);
- alert('item1:'+item1);
- alert('item2:'+item2);
- }
2.用來驗證用戶名是否存在
- function verify(){
- var jqueryObj = $("#username");
- //獲取節點的值
- var userName = jqueryObj.val();
- alert(userName);
- $.get("TestAction!test.action?name=" + userName,null,callback);
- }
- function check(){
- //獲取節點的值
- var userName = $("#username").val();
- if(userName=="" || userName=="請輸入用戶名"){
- alert("用戶名不能為空");
- }else{
- $.get("TestAction!xml.action?name=" + userName,null,callback);
- }
- }
- function callback(data){
- //alert("服務器返回值:"+data);
- var resultObj = $("#result");
- // var domObj = xmlhttp.responseXML;
- var domObj = data;
- //<message>xxxx</message>
- //dom中利用getElementsByTagName可以根據標簽名獲取元素節點
- //var messageNodes = domObj.getElementsByTagName("message");
- //message標簽中的文本在dom中是message標簽所對應的元素節點的子節點,firstChild可以獲得當前節點的第一個子節點
- //var textNode = messageNodes[0].firstChirld;
- //通過nodeVale可以獲取節點的值
- //var responseMessage = textNode.nodeVale;
- var responseMessage = $(data).find("message").text();
- /*$(xml).find("message").each(function() {
- var field = $(this);
- //var fName = field.attr("Name");//讀取節點屬性
- //var dataType = field.find("datatype").text();//讀取子節點的值
- alert(field.text());
- });*/
- resultObj.html(responseMessage);
- }
- function postsubmit(){
- $.post("TestAction!test.action?name=" + $("#username").val(),null,callback);
- }
- <input id="username" name="username" value="請輸入用戶名"></input>
- <input type="button" onclick="verify()" value="確定"/>
- <input type="button" onclick="check()" value="驗證用戶名是否存在"/>
服務端代碼:
- public void test() throws Exception {
- // TODO Auto-generated method stub
- System.out.println("name:"+new String(name.getBytes("iso-8859-1"),"utf-8"));
- HttpServletResponse httpServletResponse = ServletActionContext.getResponse();
- httpServletResponse.setContentType("text/html;charset=utf-8");
- PrintWriter pw = httpServletResponse.getWriter();
- pw.print("可以注冊");
- }
3.$.ajax的簡單使用
- <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'test2.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- <script type="text/javascript" src="js/jquery-1.4.4.min.js"></script>
- <script type="text/javascript">
- function t1(){
- var jqueryObj = $("#username");
- var userName = jqueryObj.val();
- var obj = {name:"123",age:20};
- //alert(userName);
- $.ajax({
- type:"POST",
- url:"TestAction!xml.action",
- data:"name=" + userName,
- dataType : "xml",
- success:callback
- });
- }
- //回調函數
- function callback(data){
- var jqueryObj = $(data);
- var message = jqueryObj.children();
- //alert(message);
- var text = message.text();
- var resultObj = $("#aaa");
- //alert(data);
- resultObj.html(text);
- //alert('3 ');
- }
- </script>
- </head>
- <body>
- <input id="username" name="username" value="請輸入用戶名"></input>
- <input type="button" onclick="t1()" value="1"/>
- <div id="aaa"></div>
- </body>
- </html>
- public void xml() throws Exception {
- // TODO Auto-generated method stub
- System.out.println("name" + name);
- // name = new String(name.getBytes("iso-8859-1"),"utf-8");
- // System.out.println("name" + name);
- StringBuffer sb = new StringBuffer();
- HttpServletResponse httpServletResponse = ServletActionContext.getResponse();
- httpServletResponse.setContentType("text/xml;charset=utf-8");
- PrintWriter out = httpServletResponse.getWriter();
- sb.append("<xml><message>");
- if(name.equals("wangweiqiang")){
- sb.append("用戶名["+name+"]已經存在,請使用其他用戶名").append("</message></xml>");
- }else{
- sb.append("用戶名["+name+"]沒有使用").append("</message></xml>");
- }
- System.out.println(sb.toString());
- out.println(sb.toString());
- }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。