简体   繁体   English

集体数据和jQuery

[英]Collective data and jQuery

You can usually use something like this: 通常,您可以使用以下方式:

<form action="example.php" method="POST">
<input type="text" name="file_name[]">
<input type="text" name="file_name[]">
<input type="text" name="file_name[]">

to collect data under the same name, and access it in PHP like: 以相同的名称收集数据,并以如下方式在PHP中进行访问:

$file_name = $_POST["file_name"];
echo $file_name[0]; //first occurrence
echo $file_name[2]; //third occurrence

but when it comes to generating dynamic fields with jQuery, like this: 但是在使用jQuery生成动态字段时,如下所示:

$("#example_table").append("<tr><td><input type="text" name="file_name[]"></td></tr>");

and submitting it with standard submit button within POST form, the outcome differs. 并使用POST表单中的标准“提交”按钮进行提交,结果会有所不同。 Only the last occurrence gets passed but the array indexes represent consecutive letters of such, not exact, indicated field as above. 仅最后一次出现通过,但数组索引表示上述表示字段(不完全正确)的连续字母。

The question is, why and how to fix it? 问题是,为什么以及如何修复它?

You have to remove another double quotes from the html string. 您必须从html字符串中删除另一个双引号。 You have two ways for that 你有两种方法

1. $("#example_table").append("<tr><td><input type=\"text\" name=\"file_name[]\"></td></tr>");

2. $("#example_table").append('<tr><td><input type="text" name="file_name[]"></td></tr>');

Maybe this will help. 也许这会有所帮助。

You can use single quote instead of double quote for file_name . 您可以使用single quote ,而不是double quotefile_name Might be this is useful to you. 可能对您有用。

$("#example_table").append("<tr><td><input type='text' name='file_name[]'></td></tr>");

So, the problem is solved. 这样,问题就解决了。 It was all about using double quotes instead of single ones. 都是关于使用双引号而不是单引号。 Thank you for your participation and support. 感谢您的参与和支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM