提交时更改提交表单的内容 [英] Change contents of submitted form on submit

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

问题描述



我知道我可以使用submit事件来捕获这个事件。

我有一个表单,我想在提交之前对它进行特殊处理。 JQuery,但我该如何更改实际从回调中提交的值?

解决方案

有几个选项:您可以在实际提交之前替换表单字段。你可以为此添加隐藏字段。我不喜欢这个选项,因为你可能有验证问题,你必须撤销你的更改。



你可以通过AJAX发布帖子,只发布你的值,然后根据结果采取适当的措施。在这种情况下,原始帖子从不发生。这就是我可能会做的。这样,您的原始表单保持不变,您可以处理服务器端验证,而无需撤消表单上的任何内容。 $($)$($)$($)$($)$($)$自定义数据...;
$ .post($ form.attr('action'),data,function(result){
// ...根据结果做
} );
return false; //取消原始提交
});
});


I have a form, and I'd like to do special processing on it before it is submitted.

I know I can catch the "submit" event using JQuery, but how do I change the values that are actually submitted from within the callback?

解决方案

A couple of options: you could replace the form fields prior to the actual submit taking place. You could use added hidden fields for this. I don't like this option particularly since you might have validation issues where you'd have to undo your changes.

You could do the post via AJAX, posting only your values, then take some appropriate action based on the result. In this case, the original post never happens. That's what I would probably do. In this way your original form stays the same and you can handle server-side validation without having to undo anything on the form.

$(function() {
     $('form').submit( function() {
         var $form = $(this);
         var data = // ... generate your custom data ...;
         $.post( $form.attr('action'), data, function(result) {
              // ... do something based on result
         });
         return false; //cancel original submit
     });
});

这篇关于提交时更改提交表单的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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