jQuery没有在.append()之后发布表单的所有输入 [英] jQuery not posting all inputs of a form after the .append()

查看:90
本文介绍了jQuery没有在.append()之后发布表单的所有输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用jQuery方法.append()动态生成的表单。
我可以添加任意数量的新输入,文本框,cmbbox等...

I have a form generated dynamically with the method .append() of jQuery. I can add any number of new input, textbox, cmbbox, etc...

但问题是,当我执行表单的sumbit时, PHP目标没有收到添加的新输入,只是连接到append()之前表单中已经输入的变量。

But the problem is that when I do the sumbit of the form, the PHP target does not receive the new input added, but just the vars connected to the input already in the form before the append().

任何想法?



javascript:


The javascript:

$("#button").live('click',function add(){
   $("#list").append(
       '<li style="height:20px;">'
    +'<input type="text" class="text" id="prova" name="prova[]" value="prova">'+ 
       '</li>'
   );
});



Html:


The Html:

<input type="submit" id="button" value="Add input">
<form name = "form" id="form" action="post.php" method="POST">
   <ul style="width:670px;padding:0px 0px 30px 0px" id="list">
   </ul>
   <input type="submit" id="submit" value="Submit">
</form>



PHP:


The PHP:

<?php
  print_r($_POST);
?>


推荐答案

问题1:



您的 #button 不应为提交类型,因为您只想使用它来添加到表单而不提交表单。所以你应该:

Problem 1:

Your #button should not be of type submit, since you just want to use it to add to the form and not submit the form. So you should have:

<input type="button" id="button" value="Add input">


您正在覆盖变量。 名称是与表单一起发送的变量,因此每个输入添加必须有一个新名称,或者变量必须是一个数组。

You are overwriting your variables. The name is the variable sent with the form, so each input addition must have a new name, or the variable must be an array.

此外,您不能拥有多个具有相同 id 的元素。

Additionally, you can't have more than one element with the same id.

解决此问题的最简单方法是使用格式使 prova 数组prova []

The simplest way to solve this is to make prova an array by using the form prova[]:

$("#button").live('click',function() {
   $("#list").append(
       '<li style="height:20px;">' +
         // Removed repetitive ID and made prova an array
       '<input type="text" class="text" name="prova[]" value="prova"></li>'
   ); 
});

这篇关于jQuery没有在.append()之后发布表单的所有输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆