jQuery获取表单值,多个表单 [英] jquery get form values, multiple forms
问题描述
页面上有多个表单,我需要获取提交的表单的值,但无需指定表单.
$(document).ready( function (){
$('form').submit( function(){
return submitForm()
});
});
并在.js文件中,submitForm()应标识所提交表单的ID.怎么办?
我得到的是[0]形式的值,而不是被单击的形式
不确定submitForm()
的作用,但我认为您正在某种程度上使用this
.
如果是这样,请尝试:
$('form').submit( function(){
return submitForm.call( this );
});
以这种方式进行操作应将被单击的表单的上下文发送到submitForm()
调用.
因此,在submitForm()
中,您可以使用this
引用正确的格式,或者在$(this)
中引用jQuery对象.
以下是我的意思的示例: http://jsfiddle.net/pXwTE/ >
I have multiple forms on the page and i need to get the values of the form that is submitted, but without specifying the form.
$(document).ready( function (){
$('form').submit( function(){
return submitForm()
});
});
and in the .js file submitForm() should identify the id of the form that is submitted. How can be that done?
I get the values of the form[0], not the one that is clicked
Not sure what submitForm()
does, but I assume you're somehow using this
in it.
If so, try:
$('form').submit( function(){
return submitForm.call( this );
});
Doing it this way should send the context of the form that was clicked to the submitForm()
call.
So in submitForm()
you can refer to the correct form using this
, or in a jQuery object using $(this)
.
EDIT:
Here's an example of what I mean: http://jsfiddle.net/pXwTE/
这篇关于jQuery获取表单值,多个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!