2016-06-30 44 views
0

我想要形成數據參數正確調用ajax在jsp上形成ajax數據示例

<script type="text/javascript"> 
    $(document).ready(function() { 
     $('#call').click(function() 
     { 
      $.ajax({ 
       type: "post", 
       url: "books", //this is my servlet 
       data: <<< my data here >>> 
      }); 
     }); 

    }); 
</script> 

這是我jsp部分:

<form action="books" method="post"> 
    <table width="70%" border="1"> 
     <% 
      List<Book> books = (List<Book>) request.getAttribute("books"); 

      for (int i = 0; i < books.size(); i++) { 
     %> 
     <tr> 

      <td> 
       <input type="checkbox" name="book<%=i%>" 
         value="<%= books.get(i).getBook_id()%>"> <%= books.get(i).getName() %> 
      </td> 

     </tr> 
     <% 
      } 
     %> 
    </table> 

    <select name="user_name"> 
     <% 
      List<User> users = (List<User>) request.getAttribute("users"); 
      for (int i = 0; i < users.size(); i++) { 
     %> 
     <option value="<%=users.get(i).getName()%>"><%=users.get(i).getName()%></option> 
     <% 
      } 
     %> 
    </select> 
    <input type="submit" name="submit" value="Purchase"> 
    <input type="button" value="Call Servlet" name="Call Servlet" id="call"/> 
</form> 

我想傳遞通常由上述form通過一切。 請問您能通過這個例子向我展示ajax技術嗎?

+0

可能重複使用:http://stackoverflow.com/questions/1960240/jquery-ajax-submit-form – meepzh

回答

1

舉一個instance id的形式和與serialize()方法

 $('#form').submit(function() 
     { 
      $.ajax({ 
       type: "post", 
       url: "books", //this is my servlet 
       data: $(this).serialize() 
      }); 
     }); 


    <form id="form" action="books" method="post"> 
     <table width="70%" border="1"> 
      <% 
       List<Book> books = (List<Book>) request.getAttribute("books"); 

       for (int i = 0; i < books.size(); i++) { 
      %> 
      <tr> 

       <td> 
        <input type="checkbox" name="book<%=i%>" 
          value="<%= books.get(i).getBook_id()%>"> <%= books.get(i).getName() %> 
       </td> 

      </tr> 
      <% 
       } 
      %> 
     </table> 

     <select name="user_name"> 
      <% 
       List<User> users = (List<User>) request.getAttribute("users"); 
       for (int i = 0; i < users.size(); i++) { 
      %> 
      <option value="<%=users.get(i).getName()%>"><%=users.get(i).getName()%></option> 
      <% 
       } 
      %> 
     </select> 
     <input type="submit" name="submit" value="Purchase"> 
     <input type="button" value="Call Servlet" name="Call Servlet" id="call"/> 
    </form>