JQuery Form AjaxSubmit(options)在Asp.net中的應用注意事項

來源:互聯網
上載者:User

所需引用的JS:

在http://www.malsup.com/jquery/form/#download 下載:http://malsup.github.com/jquery.form.js

在http://jquery.com/ 下載:http://code.jquery.com/jquery-1.7.2.min.js

注意事項:

//dataType: "json",       //get的方式再設定此屬性

//注意:from 如果是 runat="server" 那option的url只能是提交給自己的.aspx,如果不是則可以提交給其他.aspx接收。
//注意:from中的<input 標籤 必須帶有name屬性,否則只有id Request.Form[] 會獲得不到後增加的標籤。

//不先Clear的話會返回整個頁面的html檔案內容,也不要用Response.Write();應該是:HttpContext.Current.Response.Write,注意HttpContext.Current.Response.ContentType = "text/html";

HttpContext.Current.Response.Clear();

範例程式碼:

aspx頁<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JqueryFormAjaxSubmit.aspx.cs" Inherits="GaryTestPro.JqueryFormAjaxSubmit" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <script type="text/javascript" src="JS/JQuery/jquery-1.7.1.min.js"></script> 
    <script type="text/javascript" src="JS/JQuery/jquery.form.js"></script> 
    <script type="text/jscript"language="jscript">
        function SubInfo() {
        
            $("#Order").append("<input name=txtDns value=\"DNS解析\" id=txtDns type=\"text\" />");
            $("#Order").append("<input name=txtIP value=\"IP地址\" id=txtIP type=\"text\" />");
            
            var options = {
                beforeSubmit: function() {
                    return true;
                },
                url: 'JqueryFormAjaxSubmit.aspx?Mode=SF',
                type: 'POST',
                //dataType: "json",       //get的方式再設定此屬性
                success: function(data) {
                    if (data == "") {
                        document.getElementById('Order').style.display = "none";
                        alert("操作成功!");
                    }
                    else {
                        alert(data);
                    }
                },
                error: function() {
                    //請求出錯處理
                    alert("error");
                }
            };
            //注意:from 如果是 runat="server" 那option的url只能是提交給自己的.aspx,如果不是則可以提交給其他.aspx接收。
            //注意:from中的<input 標籤 必須帶有name屬性,否則只有id Request.Form[] 會獲得不到後增加的標籤。
            $("#form1").ajaxSubmit(options);
        }
    </script> 
</head>
<body>
    <form id="form1" runat="server" method="post">
    <div>
        <div id="Order">
            
        </div>
        <input name="txtName" value="星期一" id="txtName" type="text" />
        <input name="txtUser" value="星期二" id="txtUser" type="text" />
        <a href="javascript:void(0);" onclick="SubInfo();">提交</a>
    </div>
    </form>
</body>
</html>

 aspx.cs 代碼:

protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Form["txtName"] != null)
            {
                string sName = Request.Form["txtName"].ToString();
            }
            if (Request.Form["txtDns"] != null)
            {
                string sDns = Request.Form["txtDns"].ToString();
                //不先Clear的話會返回整個頁面的html檔案內容
                HttpContext.Current.Response.Clear();

                HttpContext.Current.Response.ContentType = "text/html";

                HttpContext.Current.Response.Write("{result:true}");

                HttpContext.Current.Response.End();
            }
            
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.