zoukankan      html  css  js  c++  java
  • JQuery Form AjaxSubmit(options)在Asp.net中的应用注意事项

    所需引用的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();
                }
  • 相关阅读:
    关于js计算非等宽字体宽度的方法
    [NodeJs系列]聊一聊BOM
    Vue.js路由管理器 Vue Router
    vue 实践技巧合集
    微任务、宏任务与Event-Loop
    事件循环(EventLoop)的学习总结
    Cookie、Session和LocalStorage
    MySQL 树形结构 根据指定节点 获取其所在全路径节点序列
    MySQL 树形结构 根据指定节点 获取其所有父节点序列
    MySQL 创建函数报错 This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators
  • 原文地址:https://www.cnblogs.com/shiyh/p/6705634.html
Copyright © 2011-2022 走看看