在不定义表单名称的情况下访问提交功能下的表单元素



我确实有一个表单,并通过提交函数提交。Bur用于提交变量我不使用文档id,而是使用类。

$(".modalform").submit(function(event) {
/* stop form from submitting normally */
event.preventDefault();
// get all the inputs into an array.
var $inputs = $('this :input');
console.log($inputs.val())
/* get the action attribute from the <form action=""> element */
var $form = $(this),
url = $form.attr('action');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

可以看出,这个提交函数不是基于id的,而是基于类的。我确实有多个具有不同id和相同类名的表单,当我提交每个表单时,我希望将它们的元素和值放在一个数组中。有这个答案,但它的形式是一个论点。我想要一种方法,可以通过任何方式获得提交的表单id,并根据它序列化值(在获得表单id后可以使用上面提到的答案(。

您可以获得类似的值

$(".modalform").submit(function(event) {
/* stop form from submitting normally */
event.preventDefault();
// get all the inputs into an array.
var $inputs = $(this).find(':input');
const vals = $inputs.map(function() { return this.value }).get()
console.log(vals)
/* get the action attribute from the <form action=""> element */
var $form = $(this),
url = $form.attr('action');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

最新更新