用jqery實現簡單ajax的例子

來源:互聯網
上載者:User

一、引言

   專案經理分配給一個活,用ajax來實現一個功能。原來沒有接觸過ajax,只知道jquery提供了ajax的介面,而我對jquery還算比較熟悉,所以現在就想學一下用jquery來實現ajax的功能。

二、功能概述

   ajax主要的功能就是“不重新整理頁面改變頁面內容”,或者就是“動態重新整理頁面”,反正我覺得就應該是胖用戶端之類的概念吧。反正現在的技術就是要讓IE功能越來越豐富,而且減少讓使用者等待重新整理頁面的機會,包括現在的actionScript都是這樣的概念吧。反正這是我自己的理解。

   我要實現的功能很簡單,就是有一個下拉式清單,列表中有三種圖書的種類,選擇一條,就可以顯示該類圖書的詳細資料,而不需要重新整理頁面或者轉換頁面。

三、開始例子

  1.資料庫表結構(mysql資料庫)

     1)建表語句:

  1.  CREATE 
  2.         TABLE catlog 
  3.         ( 
  4.             catlogid int(11) NOT NULL AUTO_INCREMENT, 
  5.             catlogname varchar(20) NOT NULL, 
  6.             mainBook text, 
  7.             supplyCompany text, 
  8.             amount int(11), 
  9.             PRIMARY KEY USING BTREE (catlogid) 
  10.         ) 
  11.             ENGINE= InnoDB DEFAULT CHARSET= utf8

     2)表中資料為:

        1 computer 作業系統、java開發、資料庫技術 北京圖書印刷公司 100
        2 ecnomic 宏觀經濟、微觀經濟、市場營銷 人民郵電出版社 200
        3    life 浮沉 新華書店 300

           

  2.jsp頁面為:

  

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  2. <%@  taglib prefix="ww"  uri="/webwork" %>
  3. <% String webapp=request.getContextPath(); %>
  4. <html>
  5.    <head>
  6.     <link href="<%=webapp%>/etc/css/common.css" rel="stylesheet" type="text/css">
  7.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8.     <SCRIPT language="javascript" src="<%=webapp%>/etc/js/jquery.js"></SCRIPT>
  9.     <title>網路書城</title>
  10.     
  11.     <script type="text/javascript">
  12.       
  13.          function setValue(){
  14.             
  15.            // document.getElementById("detail").style="visibility:visible ;"
  16.             $('#list').show();
  17.             $('#mainBook').attr("value","${catalog.mainBook}");
  18.             $('#supplyCompany').attr("value","${catalog.supplyCompany}");
  19.             $('#amount').attr("value","${catalog.amount}");
  20.          }
  21.       
  22.       
  23.         function viewDetail(name){
  24.              $.getJSON(
  25.                  "getCatalogInfo.action",
  26.                  { catalogName: name },
  27.                  function(msg){
  28.             $('#mainBook').attr("value",msg.mainBook);
  29.             $('#supplyCompany').attr("value",msg.supplyCompany);
  30.             $('#amount').attr("value",msg.amount);
  31.                  }   
  32.              );
  33.         }
  34.     </script>
  35.    </head>
  36.    <body>  
  37.        <div id="ajax" class="list" style="display:block">
  38.              <form id="frm" action="getCatalogInfo.action">
  39.                <table >
  40.                    <tr>
  41.                       <td>請選擇圖書種類:
  42.                       <select onchange="viewDetail(this[selectedIndex].text)"> 
  43.                         <ww:iterator id="catalogList"  value="catalogList">
  44.                          <option value="${catlogname}">${catlogname} </option>
  45.                         </ww:iterator>
  46.                       </select>
  47.                       </td>
  48.                    </tr>
  49.                </table>
  50.               <table>
  51.                 <tr><td>主要圖書:<input id="mainBook" type="text" value=""/></td></tr>
  52.                 <tr><td>主要供應商:<input id="supplyCompany" type="text" value=""/></td></tr>
  53.                 <tr><td>本書店擁有該圖書數量:<input id="amount" type="text" value=""/></td></tr>
  54.               </table>
  55.             </form>
  56.        </div>
  57.            
  58.    </body>
  59. </html>

3.action代碼

  1.     public String getCatalogInfo(){
  2.         
  3.         catalog=ICatalogService.getCatlogByName(catalogName);
  4.         JSONObject json= new JSONObject();
  5.         try {
  6.             json.put("mainBook",catalog.getMainBook());
  7.             json.put("supplyCompany", catalog.getSupplyCompany());
  8.             json.put("amount", catalog.getAmount());
  9.         } catch (JSONException e) {
  10.             e.printStackTrace();
  11.             return ERROR;
  12.         }
  13.         
  14.         catalogName = json.toString();
  15.         
  16.         return SUCCESS;
  17.     }

4.service層代碼:

  1.     public Catlog getCatlogByName(String name) {
  2.         
  3.        return ICatalogDAO.getCatlog(name);
  4.     }

 

5.pojo

   

  

  1. package cn.resoft.cm.vo;
  2. import java.util.HashSet;
  3. import java.util.Set;
  4. public class Catlog implements java.io.Serializable {
  5.     private Integer catlogid;
  6.     private String catlogname;
  7.     private String mainBook;
  8.     private String supplyCompany;
  9.     private Integer amount;
  10.     private Set books = new HashSet(0);
  11.     public Catlog() {
  12.     }
  13.     public Catlog(String catlogname) {
  14.         this.catlogname = catlogname;
  15.     }
  16.     public Catlog(String catlogname, Set books) {
  17.         this.catlogname = catlogname;
  18.         this.books = books;
  19.     }
  20.     public Integer getCatlogid() {
  21.         return this.catlogid;
  22.     }
  23.     public void setCatlogid(Integer catlogid) {
  24.         this.catlogid = catlogid;
  25.     }
  26.     public String getCatlogname() {
  27.         return this.catlogname;
  28.     }
  29.     public void setCatlogname(String catlogname) {
  30.         this.catlogname = catlogname;
  31.     }
  32.     public Set getBooks() {
  33.         return this.books;
  34.     }
  35.     public void setBooks(Set books) {
  36.         this.books = books;
  37.     }
  38.     public String getMainBook() {
  39.         return mainBook;
  40.     }
  41.     public void setMainBook(String mainBook) {
  42.         this.mainBook = mainBook;
  43.     }
  44.     public String getSupplyCompany() {
  45.         return supplyCompany;
  46.     }
  47.     public void setSupplyCompany(String supplyCompany) {
  48.         this.supplyCompany = supplyCompany;
  49.     }
  50.     public Integer getAmount() {
  51.         return amount;
  52.     }
  53.     public void setAmount(Integer amount) {
  54.         this.amount = amount;
  55.     }
  56. }

 

 

好了,這個功能就實現了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.