简体   繁体   English

jQuery:form.submit(fn)不能与Asp.net一起使用?

[英]jQuery: form.submit(fn) does not work with Asp.net?

I am trying to attach an event handler to form.submit on asp.net rendered pages with no success. 我试图在asp.net呈现的页面上附加一个事件处理程序到form.submit但没有成功。 I want to intercept every postback, and doc. 我想拦截每一个回发和doc。 says that I should be able. 说我应该能够。 Am I doing something wrong? 难道我做错了什么?

$(document).ready(function() {
    $("form").submit(function() {
            alert('Form submit');
            debugger;
    });
});

Don't know if you are still looking for it, but here is the solution I have. 不知道你是否还在寻找它,但这是我的解决方案。 I don't think it works with event canceling if using, say, stopPropagation() but it will let you run some script before submitting the form. 如果使用stopPropagation(),我认为它不适用于事件取消,但它会让你在提交表单之前运行一些脚本。

<script type="text/javascript">
    $(function () {
        var oldSubmit = __doPostBack;
        var newSubmit = function (eventTarget, eventArgument) {
            alert('custom submit function');
            return oldSubmit(eventTarget, eventArgument);
        };
        __doPostBack = newSubmit;
    });
</script>

This is something I did on a legacy WebForm application: 这是我在遗留WebForm应用程序上所做的事情:

//trigger jquery form submit event handlers on __doPostBack
$(function() {
    var oldPostBack = __doPostBack;
    __doPostBack = function() {
        $("form").triggerHandler("submit");
        oldPostBack.apply(this, arguments);
    };
});

This allows you to wire "submit" events using jQuery the way you normally would: 这允许您以通常的方式使用jQuery连接“提交”事件:

$("#myForm").submit(function() { alert("submitted!"); });

Typical doPostBack hijacking, but only once. 典型的doPostBack劫持,但只有一次。 It triggers "submit" handlers on any form elements before doing the old postback. 它在执行旧回发之前触发任何表单元素上的“提交”处理程序。 This is necessary because, while asp.net does call the onsubmit function attached to the form dom object , it doesn't look like jquery's events work that way. 这是必要的,因为虽然asp.net确实调用了附加到表单dom对象的onsubmit函数 ,但它看起来并不像jquery的事件那样工作。

asp.net webforms are generally enveloped in only one big form (hence the term web forms). asp.net webforms通常只包含在一个大表单中(因此称为web表单)。

if I'm not mistaken, all links and submit buttons call __doPostBack manually, so it bypasses the calling form submit and delegates that responsibility to the __doPostBack function. 如果我没弄错,所有链接和提交按钮都会手动调用__doPostBack,因此它会绕过调用表单提交并将该责任委托给__doPostBack函数。

you would probably need to overwrite this function. 你可能需要覆盖这个功能。

<script type="text/javascript"> 
//<![CDATA[
var theForm = document.forms['aspnetForm'];
if (!theForm) {
    theForm = document.aspnetForm;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script> 

though I'm trying to understand your "debugger;" 虽然我想了解你的“调试器”; line in your function. 在你的功能行。 perhaps it should be this? 也许它应该是这个?

$(document).ready(function() {
    $("form").submit(function() {
            alert('Form submit');
    });
}); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM