使用jQuery和PHP动态加载textareas数组时遇到问题 [英] Problem processing an array of textareas when they are loaded dynamically with jQuery and PHP
问题描述
我在尝试在表单上进行多文本区域处理时遇到问题.
I am experiencing problems trying to do a multitextarea on a form.
我有一个PHP视图,该视图具有各种形式,所有处理方式不同,但都在同一文件中.它们都可以工作,我已经测试过并且可以工作.在表单中,有一个+图标,允许用户添加<textarea />
的更多实例.该+图标通过AJAX加载与先前文本区域名称相同的新文本区域(例如,name ="example []").如果我发布此内容,它将仅获得第一个项目,该项目是jQuery之前未加载的项目.问题在于,动态加载的内容被完全忽略了,我也不知道为什么.
I've a PHP view that has various forms, all processed different but in the same file. They all works, I've tested and they works. In forms there are a + icon that let the user add more instances of a <textarea />
. This + icon loads via AJAX a new textarea with the same name of the previous textarea (for example, name="example[]"). If I post this, it only gets the first item that is the one that wasn't previously loaded by jQuery. The problem is that the ones loaded dynamically are totally ignored and I don't know why.
我的PHP视图摘录:
<?php echo form_open("projects/view/".$projectid); ?> <!-- This generates a valid <form /> tag -->
<table>
<tr>
<th><?php echo lang("label_conx");?></th>
<td class="textarea-edit">
<textarea class="context" name="CONX[]"><?php echo set_value("CONX[]");?></textarea>
</td>
<td class="add"><a id="add-conx" href="#"><?php echo img("img/icons/plus.png");?></a></td>
</tr>
...
我的jQuery代码可以正常工作,因为显示了内容,并且在萤火虫中看到的内容与html加载的textarea相同:
My jQuery code that works fine because the content is displayed and I see in firebug that is the same as the html loaded textarea:
$("a#add-conx").click(function(){
$("#ajax-loader").fadeIn('normal');
$("textarea.context:last").after('<textarea class="context" name="CONX[]"></textarea>');
$("#ajax-loader").fadeOut("normal");
return false;
});
当我发送表单时,我在我的PHP控制器print_r($_POST);
中有一个,我只能得到:
And when I send the form, I've a in my PHP controller print_r($_POST);
and I only get:
Array
(
[CONX] => Array
(
[0] => safdsddasfafsd
)
...
如果我在PHP视图上放置各种文本区域,则此数组的增加与在HTML视图文件上创建的文本区域的数目相同.我不明白为什么这行不通.
If I put on the PHP view various textareas, this array increases as same of the number of textareas I've created on the HTML view file. I don't understand why this is not working.
加载页面时:
动态添加一些内容之后:
After adding some content dynamically:
这两个图像显示了名称为context []的textarea的4个实例. (已更改为CONX [],但失败相等)在视图中.第二张图片显示的是使用jQuery和PHP加载的图片,仅检测到HTML上的前4个图片.
This two images shows 4 instances of textarea with the name context[] (that has been changed to CONX[], but fails equal) that are in the view. The second image shows the loaded ones with jQuery and PHP only detected the first 4 that were on the HTML.
有人知道我该如何解决吗?
Anyone has some idea how I can solve this?
提前谢谢!
推荐答案
我怀疑这与您的html的正确性有关.
I suspect this has something to do with the correctness of your html.
例如,阅读您的问题以及您使用常规提交的事实,这可能是缺少表单"结尾标签的原因.
Reading your question and the fact that you use a regular submit the problem could be a missing 'form' end-tag for instance.
当未正确嵌套html时,浏览器有时会具有怪异的Javascript行为.
Browsers sometimes have weird Javascript behaviors when the html is not properly nested.
这篇关于使用jQuery和PHP动态加载textareas数组时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!