动态创建的索引字段未随表单提交

发布于 2025-01-04 12:13:29 字数 1389 浏览 2 评论 0原文

我使用 jQuery 动态创建电子邮件地址的新字段,其中包含一个用于指定电子邮件地址类型的选择下拉框和一个电子邮件地址的文本输入。

HTML

<form enctype="multipart/form-data" action="handler.php" method="post" />
<ul class="ul-lov">
<li>
    <select name="email_type_id[]" class="float-l edit-email-type">
      <option value="1">No Label</option>
      <option value="5">Email</option>
      <option value="10" selected="selected">Personal</option>
      <option value="20">Work</option>
      <option value="30">Other</option>
    </select>
    <input type="text" name="email_value[]" value="[email protected]" class="float-l edit-email"> 
    <img src="/gui/minus-button.png" class="minus">
    <img src="/gui/plus-button.png" class="addlov">
</li>
</ul>
</form>

jQuery: 当用户单击“plus-button.png”时,它将运行以下代码。本质上,它只是创建当前 LI 的副本并删除其中的值。

// Click plus
$('.addlov').live('click', (function() {        
    $(this).closest('ul').append(
        $(this).parent('li').clone(true).children('input, select').val('').end()
    );
    $(this).remove();
}));

这一切都运行良好,问题是当我提交表单时,动态创建的字段被忽略。提交时,我将序列化表单数据并显示在警报中以验证这一点。我已经尝试了一切,似乎看不出问题是什么!?有什么想法吗?

I'm using jQuery to dynamically create new fields for email addresses which consist of a SELECT dropdown box to specify the type of email address and a text INPUT for the email address.

HTML

<form enctype="multipart/form-data" action="handler.php" method="post" />
<ul class="ul-lov">
<li>
    <select name="email_type_id[]" class="float-l edit-email-type">
      <option value="1">No Label</option>
      <option value="5">Email</option>
      <option value="10" selected="selected">Personal</option>
      <option value="20">Work</option>
      <option value="30">Other</option>
    </select>
    <input type="text" name="email_value[]" value="[email protected]" class="float-l edit-email"> 
    <img src="/gui/minus-button.png" class="minus">
    <img src="/gui/plus-button.png" class="addlov">
</li>
</ul>
</form>

jQuery:
When the user clicks the "plus-button.png", it will run the following code. Essentially it just creates a copy of the current LI and strips the values.

// Click plus
$('.addlov').live('click', (function() {        
    $(this).closest('ul').append(
        $(this).parent('li').clone(true).children('input, select').val('').end()
    );
    $(this).remove();
}));

This all works good and well, the problem is that when I submit the form, the dynamically created fields are ignored. On submit, I'm serializing the form data and displaying in an alert to verify this. I've tried everything, can't seem to see what the issue is!? Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

笨笨の傻瓜 2025-01-11 12:13:29

我想通了...

我上面发布的表单片段是一个更大的应用程序的一小部分。该应用程序仍然只使用页面上的一种表单。不同的 div 中有许多表单元素。它不起作用的原因是因为我的 FORM 标签不在所有 div 之外。

这是我的代码如何设置的示例:

<div id="topform">
    <form action="handler.php" method="post">
    // some form items
</div>
<div id="moreformstuff">
    // some form items
</div>
<div id="bottomformstuff">
    // some form items
    </form>
</div>

所以我像这样移动标签:

<form action="handler.php" method="post">
<div id="topform">        
    // some form items
</div>
<div id="moreformstuff">
    // some form items
</div>
<div id="bottomformstuff">
    // some form items
</div>
</form>

现在一切正常。 :/希望这可以帮助其他人不要在同样的问题上浪费时间!

I figured it out...

The form snippet I posted above was a small part of a much larger app. The app still only uses one form on the page. There are numerous form elements in different divs. The reason it wasn't working is because my FORM tag was not outside of all of the divs.

Here's an example of how my code was set up:

<div id="topform">
    <form action="handler.php" method="post">
    // some form items
</div>
<div id="moreformstuff">
    // some form items
</div>
<div id="bottomformstuff">
    // some form items
    </form>
</div>

So I moved my tags around like this:

<form action="handler.php" method="post">
<div id="topform">        
    // some form items
</div>
<div id="moreformstuff">
    // some form items
</div>
<div id="bottomformstuff">
    // some form items
</div>
</form>

And everything now works fine. :/ Hope this helps someone else to not waste hours with the same issue!

静待花开 2025-01-11 12:13:29

抛出一个

上 handler.php 并查看传递了什么。当我测试您的示例时,单击加号一次并填写后,我在 handler.php 上得到以下信息:

array(2) {
  ["email_type_id"]=>
  array(2) {
    [0]=>
    string(2) "10"
    [1]=>
    string(2) "30"
  }
  ["email_value"]=>
  array(2) {
    [0]=>
    string(14) "[email protected]"
    [1]=>
    string(15) "[email protected]"
  }
}

在发布到 handler.php 之前,您对数据执行其他操作吗?

Throw a <pre><?php var_dump($_POST); ?></pre> up on handler.php and see what does get passed. When I test out your example, after clicking the plus sign once and filling it out, I get this on handler.php:

array(2) {
  ["email_type_id"]=>
  array(2) {
    [0]=>
    string(2) "10"
    [1]=>
    string(2) "30"
  }
  ["email_value"]=>
  array(2) {
    [0]=>
    string(14) "[email protected]"
    [1]=>
    string(15) "[email protected]"
  }
}

Are you doing anything else to the data before posting to handler.php?

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