表单序列化,jq中的serialize

本文探讨了jQuery的serialize方法,该方法用于将表单数据转换为URL序列化字符串,便于发送到服务器。通过示例和相关资源链接,深入理解表单序列化的应用场景和实现方式。
摘要由CSDN通过智能技术生成
<!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>
    <title></title>
    <script type="text/javascript" src="lib/jquery-1.7.2.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#btnSubmit").click(function () {
                $.ajax({
                    type: "post",
                    url: "Handler02.ashx",
                    data: $("#frmUserInfo").serialize(),
                    success: function (result) {
                        $("#msg").html(result);
                    }
                });

                return false;
            });
        });
    </script>
</head>
<body>
    <form id="frmUserInfo" action="#">
        <table>
            <tr>
                <td>
                    姓名
                </td>
                <td>
                    <input type="text" id="txtUserName" name="txtUserName" />
                </td>
            </tr>
            <tr>
                <td>
                    密码
                </td>
                <td>
                    <input type="password" id="txtUserPwd" name="txtUserPwd" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="submit" id="btnSubmit" value="提交" />
                </td>
            </tr>
        </table>
    </form>
    <div id="msg"></div>
</body>
</html>

 /// <summary>
    /// Handler 的摘要说明
    /// </summary>
    public class Handler02 : IHttpHandler
    {
        /// <summary>
        /// 请求处理
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            // 方式2(对应表单序列化)
            string userName = context.Request.Form["txtUserName"].ToString();
            string userPwd = context.Request.Form["txtUserPwd"].ToString();

            context.Response.Write(string.Format("姓名:{0},密码:{1}", userName, userPwd));
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

var array=$("form1").serializeArray();//直接序列化为json数组
array['requestType']=type;//添加请求类型的键值
 
$.post("AddProductInfo.aspx",array,function(result){//直接把追加值之后的Json数组传过去就好了
   //pass
})
 
//在aspx页面直接使用this.Request.Form['键值']来获取至即可


ps:另参考http://blog.csdn.net/zhangdaiscott/article/details/18456215


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值