动态创建的索引字段未随表单提交
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想通了...
我上面发布的表单片段是一个更大的应用程序的一小部分。该应用程序仍然只使用页面上的一种表单。不同的 div 中有许多表单元素。它不起作用的原因是因为我的 FORM 标签不在所有 div 之外。
这是我的代码如何设置的示例:
所以我像这样移动标签:
现在一切正常。 :/希望这可以帮助其他人不要在同样的问题上浪费时间!
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:
So I moved my tags around like this:
And everything now works fine. :/ Hope this helps someone else to not waste hours with the same issue!
抛出一个
上 handler.php 并查看传递了什么。当我测试您的示例时,单击加号一次并填写后,我在 handler.php 上得到以下信息:
在发布到 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:Are you doing anything else to the data before posting to handler.php?