$('#myForm').submit(function() {
// get all the inputs into an array.
var $inputs = $('#myForm :input');
// not sure if you wanted this, but I thought I'd add it.
// get an associative array of just the values.
var values = {};
$inputs.each(function() {
values[this.name] = $(this).val();
});
});
感谢Simon_Weaver的提示,这里有另一种方法,你可以用
serializeArray
来做。注意,这个片段在" "元素上会失败。
在jQuery 1.3版本中,new HTML 5 form inputs似乎不能与
serializeArray
一起工作。这在1.4以上版本中可以使用elements变量将包含表单中所有的输入、选择、文本区域和字段集。
关联性?不做一些工作是不行的,但你可以使用通用选择器。