简体   繁体   English

jQuery不会在.append()之后发布表单的所有输入

[英]jQuery not posting all inputs of a form after the .append()

I have a form generated dynamically with the method .append() of jQuery. 我有一个使用jQuery方法.append()动态生成的表单。 I can add any number of new input, textbox, cmbbox, etc... 我可以添加任意数量的新输入,文本框,cmbbox等。

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(). 但是问题是,当我执行表单的求和时,PHP目标不会收到添加的新输入,而只是在append()之前的表单中已连接到输入的var。

Any ideas? 有任何想法吗?


The javascript: JavaScript:

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


The Html: 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>


The PHP: PHP:

<?php
  print_r($_POST);
?>

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. 你的#button不应该是类型的submit ,因为你只是想用它来添加到窗体,而不是提交表单。 So you should have: 因此,您应该具有:

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


Problem 2: 问题2:

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. name是随表格发送的变量,因此每个input附加项都必须具有新名称,或者变量必须是数组。

Additionally, you can't have more than one element with the same id . 此外,具有相同id元素不能超过一个。

The simplest way to solve this is to make prova an array by using the form prova[] : 解决此问题的最简单方法是使用形式prova[]使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>'
   ); 
});

jsFiddle example jsFiddle示例

Just to clarify, and putting any other problems aside, @Claudio's note is the correct answer here. 只是为了澄清一下,并将其他问题放在一边,@ Claudio的注释是此处的正确答案。 I just had the same problem, button type was 'button' and the new element's name was being dynamically incremented. 我只是遇到了同样的问题,按钮类型为“按钮”,并且新元素的名称正在动态增加。 Everything looked fine, but the added elements would not submit . 一切看起来都不错,但是添加的元素不会提交

Then I noticed my form tags were inside the table tags. 然后我注意到我的表格标签在表格标签内。 I moved them outside and it all worked as planned. 我把它们移到外面,一切都按计划进行。

You are intercepting the click event and adding elements to the form, but the event has already started, and will complete its default action (submit the form) without re-checking the content of the form. 您正在拦截click事件并向表单添加元素,但是该事件已经开始,并且将在不重新检查表单内容的情况下完成其默认操作(提交表单)。

You should stop the event after adding the fields (preventDefault should be the right choice), and then re-submit the form. 添加字段后,应该停止事件(preventDefault应该是正确的选择),然后重新提交表单。

Something along these lines: 遵循以下原则:

$('#button').live('click', function add(event){
    event.preventDefault();
    $('#list').append(...);
    $('#form').submit();
});

I haven't tested it, but I'm pretty confident that it should work :) 我还没有测试过,但是我很有信心它应该可以工作:)

Have any code to show? 有任何代码要显示吗? In order for php to "see" the vars submitted, you have to ensure that it has the "name" attribute specified on the form elements. 为了使php“查看”提交的var,必须确保它具有在表单元素上指定的“ name”属性。 I have a feeling your issue is going to be with the jQuery not the php. 我有一种感觉,您的问题将是jQuery而不是php。

最佳猜测:您尚未为动态添加的元素设置名称属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM