发送多个序列化表单

时间:2013-08-15 02:55:29

标签: jquery serialization

我有一个问题,我有一个包含多种形式的字段,这些表单最多可重复50次,但值不同,我想发送您从列表中选择的表单,带$。 post('process.php',$(“#form”)。serialize(),function(data),但是当点击表单时,只发送第一个表单但不发送表单值进行选择。

尝试单位本身,这是两个简单的文件,如

forms.html

<html>
<head>
<title>Ejemplo de form con jquery</title> 
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
<script type="text/javascript">
$(function() {
$("form").validate( {
submitHandler: function(form) {
$.post('process.php', $(form).serialize(), function(data) {
$('#results').html(data);
            });
        }
    });
});
</script>
</head>
<body>
<form method="post" id="form">
    <label for="name" id="name_label">Nombre</label>  
    <input type="text" name="name" id="name" size="30" value=""/>  
    <br>

    <input type="submit" name="submit" value="SEND"> 
</form>

<form method="post" id="form">
    <label for="name" id="name_label">Nombre</label>  
    <input type="text" name="name" id="name" size="30" value=""/>  
    <br>
  <input type="submit" name="submit" value="SEND"> 
</form>

<form method="post" id="form">
    <label for="name" id="name_label">Nombre</label>  
    <input type="text" name="name" id="name" size="30" value=""/>  
    <br>
  <input type="submit" name="submit" value="SEND"> 
</form>

<form method="post" id="form">
    <label for="name" id="name_label">Nombre</label>  
    <input type="text" name="name" id="name" size="30" value=""/>  
    <br>
  <input type="submit" name="submit" value="SEND"> 
</form>

<form method="post" id="form">
    <label for="name" id="name_label">Nombre</label>  
    <input type="text" name="name" id="name" size="30" value=""/>  
    <br>
  <input type="submit" name="submit" value="SEND"> 
</form>
<div id="results"></div>
</body>
</html>

process.php

 <?php

    print "Form enviado correctamente: <br>the post value is <b>".$_POST['name']."</b> ";
?>

我想要的是,如果我在下面的最后一个表格中加上一个值,我会发送给你发送这个表格或你选择的任何一个的价值,而不是上面表格的价值,那就是问题出在哪里不希望q一次发送所有表单的值ls只想发送表单的值来选择

3 个答案:

答案 0 :(得分:1)

添加具有唯一名称和/或ID的表单。请注意,输入类型已从提交更改为按钮。

<form method="post">
    <label for="name" id="name_label">Nombre</label>  
    <input type="text" name="name" id="name" size="30" value=""/>  
    <input type="button" value="SEND"> 
</form>
<form method="post">
    <label for="name" id="name_label">Nombre</label>  
    <input type="text" name="name" id="name" size="30" value=""/>  
    <input type="button" value="SEND"> 
</form>

然后连线事件处理程序

$('input[type=button]').click(function(e)) {
    $(this).parent('form').submit();
};

最好不要在DOM中使用两个具有相同id的元素。

答案 1 :(得分:0)

为每个表单指定一个类或相同的名称,然后使用$(。class).serialize();

是的,你的html中的元素不应该具有相同的id attr val,这会破坏唯一标识符的用途,不良做法

答案 2 :(得分:0)

$(function() {
      $("form").each(function() {
          $(this).validate( {
              submitHandler: function(form) {
                  $.post('process.php', $(form).serialize(), function(data) {
                      $('#results').html(data);
                  });
              }
          });
      });
  });
相关问题