从提交按钮获取表单序列化数据(从几种形式)

时间:2016-02-11 19:27:17

标签: jquery forms submit

我无法通过formdata获取数据,这是一个没有问题的形式,但是像许多同一个类并没有接受它们。

但总是使用这两个不起作用

$('.form').submit(function(){ 
    alert($(this).serialize());
});

$('.submitform').click( function(e) {
  var form=$(this.form);
  alert(form.serialize());
});

修改

这是3个示例表单, 我需要获取表单的值由其类提交,但3具有相同的类

<form action="page" class="formEnviar">
<input type='text' readonly value="1" class="form-control input-sm" name="de" />
<button type="submit" class="submitform" class="submitform btn btn-primary">Enviar</button>
</form>

<form action="page" class="formEnviar">
<input type='text' readonly value="2" class="form-control input-sm" name="de" />
<button type="submit" class="submitform" class="submitform btn btn-primary">Enviar</button>
</form>

<form action="page" class="formEnviar">
<input type='text' readonly value="3" class="form-control input-sm"  name="de" />
<button type="submit" class="submitform" class="submitform btn btn-primary">Enviar</button>
</form> 

1 个答案:

答案 0 :(得分:0)

我认为你也希望得到多种形式的序列化数据。

$('#btn').click(function(){
  alert($('.form').serialize());
  $('form').submit();
})
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<form class="form" action="action_page.php">
  First name:<br>
  <input type="text" name="firstname" value="Mickey">
  <br>
  Last name:<br>
  <input type="text" name="lastname" value="Mouse">
  <br><br>
  
</form> 
  
<form class="form" action="action_page.php">
  First name:<br>
  <input type="text" name="firstname" value="Mickey">
  <br>
  Last name:<br>
  <input type="text" name="lastname" value="Mouse">
  <br><br>
</form> 
<button id="btn" type="button">Submit</button>  
</body>
</html>