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

发布于 2024-09-25 16:55:41 字数 853 浏览 3 评论 0原文

我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

沦落红尘 2024-10-02 16:55:41

问题 1:

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

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

问题2:

你正在覆盖你的变量。 name 是随表单发送的变量,因此每个input 添加都必须有一个新名称,或者变量必须是一个数组。

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

解决此问题的最简单方法是使用 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 示例

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">

Problem 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.

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 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>'
   ); 
});

jsFiddle example

孤凫 2024-10-02 16:55:41

您正在拦截 click 事件并向表单添加元素,但该事件已经启动,并将完成其默认操作(提交表单),而无需重新检查表单内容。

您应该在添加字段后停止事件(preventDefault 应该是正确的选择),然后重新提交表单。

沿着这些思路:

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

我还没有测试过它,但我非常有信心它应该有效:)

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:

$('#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 :)

面犯桃花 2024-10-02 16:55:41

只是为了澄清,并把任何其他问题放在一边,@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.

淡淡绿茶香 2024-10-02 16:55:41

有任何代码可以显示吗?为了让 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.

爱情眠于流年 2024-10-02 16:55:41

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

Best guess: You haven't set name attributes for your dynamically added elements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文