jQuery 不会在 .append() 之后发布表单的所有输入
我有一个使用 jQuery 的 .append() 方法动态生成的表单。 我可以添加任意数量的新输入、文本框、cmbbox 等...
但问题是,当我执行表单的 sumbit 时,PHP 目标不会收到添加的新输入,而只会收到连接到输入的变量已经在append()之前的表单中了。
有什么想法吗?
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:
<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:
<?php
print_r($_POST);
?>
I have a form generated dynamically with the method .append() of jQuery.
I can add any number of new input, textbox, cmbbox, etc...
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().
Any ideas?
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>'
);
});
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>
The PHP:
<?php
print_r($_POST);
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
问题 1:
您的
#button
不应该是submit
类型,因为您只想使用它添加到表单而不是提交形式。所以你应该有:问题2:
你正在覆盖你的变量。
name
是随表单发送的变量,因此每个input
添加都必须有一个新名称,或者变量必须是一个数组。此外,不能有多个元素具有相同的
id
。解决此问题的最简单方法是使用
prova[]
形式将prova
设为数组:jsFiddle 示例
Problem 1:
Your
#button
should not be of typesubmit
, since you just want to use it to add to the form and not submit the form. So you should have:Problem 2:
You are overwriting your variables. The
name
is the variable sent with the form, so eachinput
addition must have a new name, or the variable must be an array.Additionally, you can't have more than one element with the same
id
.The simplest way to solve this is to make
prova
an array by using the formprova[]
:jsFiddle example
您正在拦截
click
事件并向表单添加元素,但该事件已经启动,并将完成其默认操作(提交表单),而无需重新检查表单内容。您应该在添加字段后停止事件(preventDefault 应该是正确的选择),然后重新提交表单。
沿着这些思路:
我还没有测试过它,但我非常有信心它应该有效:)
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.You should stop the event after adding the fields (preventDefault should be the right choice), and then re-submit the form.
Something along these lines:
I haven't tested it, but I'm pretty confident that it should work :)
只是为了澄清,并把任何其他问题放在一边,@Claudio 的注释是这里的正确答案。我刚刚遇到了同样的问题,按钮类型是“按钮”,并且新元素的名称正在动态递增。一切看起来都很好,但添加的元素不会提交。
然后我注意到我的表单标签位于表格标签内。我把它们搬到外面,一切都按计划进行。
Just to clarify, and putting any other problems aside, @Claudio's note is the correct answer here. 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.
有任何代码可以显示吗?为了让 php 能够“看到”提交的变量,您必须确保它具有在表单元素上指定的“name”属性。我感觉你的问题是 jQuery 而不是 php。
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. I have a feeling your issue is going to be with the jQuery not the php.
最佳猜测:您尚未为动态添加的元素设置名称属性。
Best guess: You haven't set name attributes for your dynamically added elements.