如何捕获在ASP.NET应用程序使用jQuery提交事件? [英] How to capture submit event using jQuery in an ASP.NET application?
问题描述
我想处理使用jQuery一个格式
元素的提交
事件。
I'm trying to handle the submit
event of a form
element using jQuery.
$("form").bind("submit", function() {
alert("You are submitting!");
});
当表单提交这永远不会触发(如回发的一部分,例如,当我点击一个按钮或LinkButton控件)。
This never fires when the form submits (as part of a postback, e.g. when I click on a button or linkbutton).
有没有一种方法,使这项工作?我可以附加到触发提交的各个元素的事件,但是这不太理想 - (例如,具有自动回= TRUE,键盘快捷键等dropdownlists)有太多的可能性。
Is there a way to make this work? I could attach to events of the individual elements that trigger the submission, but that's less than ideal - there are just too many possibilities (e.g. dropdownlists with autopostback=true, keyboard shortcuts, etc.)
更新:下面是一个最小的测试案例 - 这是我的aspx页面的全部内容:
Update: Here's a minimal test case - this is the entire contents of my aspx page:
<%@ page language="vb" autoeventwireup="false" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:scriptmanager id="ScriptManager" runat="server" enablepartialrendering="true">
<scripts>
<asp:scriptreference path="/Standard/Core/Javascript/Jquery.min.js" />
</scripts>
</asp:scriptmanager>
<p>
<asp:linkbutton id="TestButton" text="Click me!" runat="server" /></p>
</div>
</form>
<script type="text/javascript">
$(document).ready(function() {
alert("Document ready.");
$("form").submit(function() {
alert("Submit detected.");
});
});
</script>
</body>
</html>
我得到的文档准备好了的警报,但不是提交检测关于LinkButton的点击时。
I get the "Document ready" alert, but not the "Submit detected" when clicking on the linkbutton.
推荐答案
谢谢,@Ken布朗宁和@russau指着我劫持__doPostBack的方向。
我见过几个不同的方法如下:
Thanks, @Ken Browning and @russau for pointing me in the direction of hijacking __doPostBack. I've seen a couple of different approaches to this:
- 硬件code我自己的版本__doPostBack的,并把它后来在页面上,使其覆盖的标准之一。
- 过载在页面上呈现,并注入自己的自定义code到现有__doPostBack。
- 充分利用JavaScript的功能性质,添加功能__doPostBack创建一个钩子。
前两个看似不理想的一对夫妇的原因(例如,假设在未来别人需要自己的功能添加到__doPostBack),所以我已经跟#3。
The first two seem undesirable for a couple of reasons (for example, suppose in the future someone else needs to add their own functionality to __doPostBack) so I've gone with #3.
本 addToPostBack
函数是一种常见的pre-jQuery的技术我以前使用的功能添加到在window.onload的变化,
和它工作得很好:
This addToPostBack
function is a variation of a common pre-jQuery technique I used to use to add functions to window.onload,
and it works well:
addToPostBack = function(func) {
var old__doPostBack = __doPostBack;
if (typeof __doPostBack != 'function') {
__doPostBack = func;
} else {
__doPostBack = function(t, a) {
if (func(t, a)) old__doPostBack(t, a);
}
}
};
$(document).ready(function() {
alert("Document ready.");
addToPostBack(function(t,a) {
return confirm("Really?")
});
});
编辑:更改addToPostBack让
Changed addToPostBack so that
- 它可以采取相同的参数__doPostBack
- 会被添加的功能发生__doPostBack 前
- 会被添加的函数可以返回false中止回发
这篇关于如何捕获在ASP.NET应用程序使用jQuery提交事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!