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