码迷,mamicode.com
首页 > Web开发 > 详细

Jquery提交form表单

时间:2016-01-22 21:06:19      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

前台代码

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Js_submit.aspx.cs" Inherits="Ctrip.Corp.Report.OfflineSite.CorpReportSetting.Js_submit" %>
 2 
 3 <html xmlns="http://www.w3.org/1999/xhtml">
 4 <head>
 5     <title>Jquery快速提交form表单</title>
 6     <link type="text/css" href="../Content/Css/StyleSheet.css" rel="Stylesheet" />
 7     <script type="text/javascript" src="../Content/js/jquery.min.js"></script>
 8     <script type="text/javascript" src="../Content/js/jquery.form.js"></script>
 9 </head>
10 <body>
11 
12 <form id="form1" action="?Action=AddCustomer" method="post">
13     <p><b>新增客户资料</b></p>
14     <span>Name: </span><input type="text" name="Name" value="abc" /><br />
15     <span>Age: </span><input type="text" name="Age" value="20" /><br />
16     <span>Address: </span><input type="text" name="Address" value="武汉" /><br />
17     <span>Tel:</span> <input type="text" name="Tel" value="12345678" /><br />
18     <span>Email: </span><input type="text" name="Email" value="test@163.com" /><br />
19     <br />
20     <input type="submit" name="btnAddCustomer" value="保存客户资料" />
21 </form>
22 
23 <hr />
24 <p><b>服务器返回的结果:</b></p>
25 <textarea id="output" cols="20" rows="50" style="width: 90%; height: 200px"></textarea>
26 
27 
28 <script type="text/javascript">
29     $(function () {
30         // 只需要下面这个调用就可以将表单改成异步提交方式!
31         var options = {
32             success: showResponse,
33             resetForm: true,
34 //            url: "../user/msg.do?d=sendMsgByAjax"
35         };
36         $("#form1").ajaxForm(options);
37     });
38      
39     function showResponse(result){//回调函数   
40        if(1 == result){   
41           alert("信息发送成功!");   
42      }else{   
43           $("#output").val(result);
44       }   
45     }  
46 
47 </script>
48 
49 </body>
50 </html>

后台代码

 1  public partial class Js_submit : Page
 2     {
 3         protected void Page_Load(object sender, EventArgs e)
 4         {
 5             string action = "";
 6             Customer customer = new Customer();
 7             if (!string.IsNullOrEmpty(Request.QueryString["Action"]))//获取form的Action中的参数
 8             {
 9                 action = Request.QueryString["Action"].Trim();//去掉空格
10             }
11 
12             switch (action)
13             {
14                 case "AddCustomer":
15                     if (!string.IsNullOrEmpty(Request.Form["Address"]) && !string.IsNullOrEmpty(Request.Form["Age"])
16                         && !string.IsNullOrEmpty(Request.Form["Email"]) && !string.IsNullOrEmpty(Request.Form["Name"]) && !string.IsNullOrEmpty(Request.Form["Tel"]))//获取form中的参数
17                     {
18                         customer.Address = Request.Form["Address"].ToString();
19                         customer.Age = Convert.ToInt32( Request.Form["Age"]);
20                         customer.Email = Request.Form["Email"].ToString();
21                         customer.Name = Request.Form["Name"].ToString();
22                         customer.Tel = Request.Form["Tel"].ToString();
23                     }
24                     AddCustomer(customer);
25                     break;
26             }
27         }
28 
29         public void AddCustomer(Customer customer)
30         {
31             // 简单地返回一个XML字符串。
32             // 告诉客户端:服务端收到了什么样的数据。
33             string ss = XMLSerializer.Serialize(customer, typeof(Customer));
34             Response.Clear();
35             Response.Write(ss);
36             Response.End();
37         }
38 
39     }

 

Jquery提交form表单

标签:

原文地址:http://www.cnblogs.com/AnXinliang/p/5151746.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!