jQuery提交解决方法 [英] jQuery submit workaround

查看:105
本文介绍了jQuery提交解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jQuery中发现了一些有趣的东西,我想让一些比我更了解它的人来运行它,看看是否有解决方案.因此,我想在页面上提交所有表单时回发页面的滚动位置.我可以通过以下操作做到这一点:

I found something interesting in jQuery, and I wanted to run it by some folks who understand it better than me to see if there is a solution for it. So I want to post back the scroll position of the page when submitting all forms on my page. I can do this by doing the following:

$('form').submit(function() {
    $(this).append('<input type="hidden" name="scroll_position" value="' + $(document).scrollTop() + '" />');
    return true;
}

当用户单击或按下提交"按钮上的Enter时,此方法可以按预期工作,但是有时会触发.submit()事件.例子:

This works as expected when user click or presses enter on the submit button, but there are times where I fire the .submit() event. Example of this:

$('button').click(function() {
    ...
    // Doing something special here
    ...
    $(this).parents('form')[0].submit();
}

无论页面是由.submit()调用还是用户提交表单启动的,无论页面提交时如何调用我的自定义提交回调都将如何进行?

How would I go about getting my custom submit callback to be called whenever the page is submitted regardless of it being initiated by a .submit() call or the user submitting a form?

推荐答案

如果不需要,无需创建新功能. 您只需要使用jquery的.submit(),您的工作就完成了.检查差异.

No need to make new function, if you don't want. You just have to use jquery's .submit() and your work is done. Check the difference.

$('button').click(function() {
    ...
    // Doing something special here
    ...
    var form = $(this).parents('form')[0];
    $(form).submit();
}

快乐编码.

这篇关于jQuery提交解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆