如何在卸载绑定表单元素之前访问jquery?

如何在卸载绑定表单元素之前访问jquery?,jquery,forms,onbeforeunload,Jquery,Forms,Onbeforeunload,如果表单未发布,我将使用jQuery显示消息,使用以下技术: $(window).bind("beforeunload", function() { form = ... // how to access the submitted $("#form")? // here is code to check if form values have changed return "Are you sure you want to leave this page without

如果表单未发布,我将使用jQuery显示消息,使用以下技术:

$(window).bind("beforeunload", function()
{
    form = ... // how to access the submitted $("#form")?
    // here is code to check if form values have changed
    return "Are you sure you want to leave this page without saving (submitting the form)?"
}
由于$(this)显然是window元素,我如何找出页面上试图提交的表单(如果表单已提交)


谢谢。

我建议您不要在卸载之前绑定到
,即使他们不尝试提交表单,也会触发该操作,而应使用绑定到表单的
提交事件的确认对话框

大概是这样的:

$('form').submit(function(e) {
    if(!confirm('Are you sure you want to submit the form?'))
        e.preventDefault();
});

我不知道该怎么做。我将替代提交:

$('form').submit(function() {
         // display message and return false if they answer 'cancel'
});

调用
$(this).submit()
,除了完全不必要之外,还会再次触发事件处理程序,导致一个无休止的循环,提示用户是否确实要提交表单,但实际上却永远无法提交。嗯,我需要使用beforeuload,尽管我的返回字符串没有意义。因此beforeunload事件与表单无关,但是如果有方法访问提交的表单(当卸载是提交表单的结果时),最好知道如何访问。无论如何,谢谢你。