发送之前可以更改表格数据吗? [英] Is it possible to change form data before sending it?
问题描述
我想找到任何通用的方法来更改数据,然后再发送表格.是的,如果我有一个<form>
包含某个类型(类)的<input>
,并且用户按下了 send 按钮,我想将此输入值转换为另一种格式,以便它到达以正确的格式发送到服务器.
I'd like to find any generic method to change data before sending forms. It is, if I have a <form>
that contains a <input>
of certain type (class) and user press send button, I'd like to transform this input value to another format so it arrive to server in a corrected format.
我知道我可以设置一个 submit()处理程序来处理数据,但是我需要一个通用的解决方案,该机制可以在所有页面表单上加载时设置此机制,而不必理会(也许有些表单已经发送了) AJAX,其他使用Jquery.validate发送,等等)
I know I can set a submit() handler to process data, but I need a generic solution that setup this mechanism on load on all page forms and forgets about it (perhaps some forms are sent by AJAX, other use Jquery.validate to send, etc)
推荐答案
使用jQuery选择所有表单元素:$('form')
并为表单submit
事件注册一个处理程序.像这样:
Use jQuery's to select all form elements: $('form')
and register a handler for the form submit
event. Something like this:
$('form').submit(function(e){
e.preventDefault()
var $this = $(this)
var formData = $this.serialize()
// do some thing to it
var yourData = transform(formData)
$.ajax({
post: $this.attr('action'),
data: yourData,
...
})
})
参考文献
- submit()
- jQuery CSS选择器
- 表单serialize()
- submit()
- jQuery CSS Selector
- Form serialize()
References
这篇关于发送之前可以更改表格数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!