读书人

asp.net中用jquery.ajaxform提交多个f

发布时间: 2012-04-30 22:33:26 作者: rapoo

asp.net中用jquery.ajaxform提交多个form当中的一个,ajaxform失效
为什么ajaxform都不好用了?连beforeSubmit都不能执行,不知道为什么,请大侠们指教


//----------------------------------------------------



WebForm1.aspx

代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!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 src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.form.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#Div2").find("form").submit(function () {
$(this).ajaxForm({
url: "/Action/Handler1.ashx",
data: { id: 2 },
dataType: 'json',
beforeSubmit: function () {
window.open("/Action/Handler1.ashx?id=2");
//alert("Div2");
},
success: function (data) {
alert(data.id);
}
});
return false;
});
$("#Div3").find("form").submit(function () {
$(this).ajaxForm({
url: "/Action/Handler1.ashx",
data: { id: 3 },
dataType: 'json',
beforeSubmit: function () {
window.open("/Action/Handler1.ashx?id=3");
//alert("Div3");
},
success: function (data) {
alert(data.id);
}
});
return false;
});

});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="Div2">
<form action="/Action/Handler1.ashx">
<textarea name="content2" id="content2" cols="50" rows="10">
</textarea>
<input type="submit" value="提交" />
</form>
</div>
</form>
<div id="Div3">
<form action="/Action/Handler1.ashx">
<textarea name="content3" id="Textarea3" cols="50" rows="10">
</textarea>
<input type="submit" value="提交" />
</form>
</div>
</body>
</html>


//----------------------------------------------------
Handler1.ashx代码:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication1.Action
{
/// <summary>
/// Handler1 的摘要说明


/// </summary>
public class Handler1 : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Buffer = true;
context.Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
context.Response.Expires = 0;
context.Response.CacheControl = "no-cache";
context.Response.AddHeader("Pragma", "No-Cache");
string id = context.Request["id"].ToString().ToLower();
context.Response.Write("{\"id\":\""+id+"\"}");
}

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

[解决办法]
ajaxForm不能提交表单。在document的ready函数中,使用ajaxForm来为AJAX提交表单进行准备。ajaxForm接受0个或1个参数。这个单个的参数既可以是一个回调函数,也可以是一个Options对象。

读书人网 >asp.net

热点推荐