您好,登錄后才能下訂單哦!
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.用來驗(yàn)證用戶名是否存在
- function verify(){
- var jqueryObj = $("#username");
- //獲取節(jié)點(diǎn)的值
- var userName = jqueryObj.val();
- alert(userName);
- $.get("TestAction!test.action?name=" + userName,null,callback);
- }
- function check(){
- //獲取節(jié)點(diǎn)的值
- var userName = $("#username").val();
- if(userName=="" || userName=="請輸入用戶名"){
- alert("用戶名不能為空");
- }else{
- $.get("TestAction!xml.action?name=" + userName,null,callback);
- }
- }
- function callback(data){
- //alert("服務(wù)器返回值:"+data);
- var resultObj = $("#result");
- // var domObj = xmlhttp.responseXML;
- var domObj = data;
- //<message>xxxx</message>
- //dom中利用getElementsByTagName可以根據(jù)標(biāo)簽名獲取元素節(jié)點(diǎn)
- //var messageNodes = domObj.getElementsByTagName("message");
- //message標(biāo)簽中的文本在dom中是message標(biāo)簽所對應(yīng)的元素節(jié)點(diǎn)的子節(jié)點(diǎn),firstChild可以獲得當(dāng)前節(jié)點(diǎn)的第一個子節(jié)點(diǎn)
- //var textNode = messageNodes[0].firstChirld;
- //通過nodeVale可以獲取節(jié)點(diǎn)的值
- //var responseMessage = textNode.nodeVale;
- var responseMessage = $(data).find("message").text();
- /*$(xml).find("message").each(function() {
- var field = $(this);
- //var fName = field.attr("Name");//讀取節(jié)點(diǎn)屬性
- //var dataType = field.find("datatype").text();//讀取子節(jié)點(diǎn)的值
- 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="驗(yàn)證用戶名是否存在"/>
服務(wù)端代碼:
- 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
- });
- }
- //回調(diào)函數(shù)
- 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+"]已經(jīng)存在,請使用其他用戶名").append("</message></xml>");
- }else{
- sb.append("用戶名["+name+"]沒有使用").append("</message></xml>");
- }
- System.out.println(sb.toString());
- out.println(sb.toString());
- }
免責(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)容。