简体   繁体   English

引用表单元素数组(名称=“ blah []”)

[英]Refering to arrays of form elements (name=“blah[]”)

I have a form with series of 3 file upload fields, each of which has a related hidden "todo" fields. 我有一个包含3个文件上传字段系列的表单,每个文件上传字段都有一个相关的隐藏“ todo”字段。

The file upload fields start greyed out and a user can either upload a new file, remove a file if one has previously been uploaded in that position or leave it unchanged (ie use the previously uploaded file or leave it blank). 文件上载字段开始显示为灰色,用户可以上载新文件,如果以前曾在该位置上载过文件,则可以删除文件,也可以保持不变(即使用先前上载的文件或将其保留为空白)。

The todo fields should store what is to be done with each file (ie 0=upload new, 1=delete existing, 2=leave unchanged). 待办事项字段应存储将对每个文件执行的操作(即0 =上载新文件,1 =删除现有文件,2 =保留不变)。

I have a series of buttons next to the upload field. 我在上传字段旁边有一系列按钮。 One for "upload new" (which enables the file upload field and (should) set the related todo field to 0; one for remove (which disables the file upload box); and one for "leave unchanged" (which also disables the file upload field). 一个用于“上传新文件”(启用文件上载字段,并将(应)将相关的待办事项字段设置为0;一个用于删除(禁用文件上载框);另一个用于“保留不变”(也禁用文件)上传字段)。

I've found the name="blah[]" technique for creating arrays when the form is posted to a PHP document which makes looping through the files nice and easy. 当表单发布到PHP文档时,我发现了用于创建数组的name =“ blah []”技术,该技术使循环浏览文件变得非常轻松。 The trouble is that I need to edit the value in the related "todo" fields and if they're all named "todo[]" then I can't refer to one specifically... 麻烦的是,我需要在相关的“ todo”字段中编辑值,如果它们都被命名为“ todo []”,那么我就不能具体引用它。

The code is something like this: 代码是这样的:

<input type="file" name="file[]" />
<input type="hidden" name="todo[]" />
<input type="button" onclick="enableFileField('file[]', 0)" value="Upload New" />
<input type="button" onclick="enableFileField('file[]', 1)" value="Remove Current" />
<input type="button" onclick="enableFileField('file[]', 2)" value="No Change" />

I'm pretty sure I'm missing something and that this is actually quite simple... 我敢肯定我会丢失一些东西,而这实际上很简单...

You can give the fields id s in addition to names. 除名称外,还可以给字段id The name would be used for the post to the server, but the id can be used for referencing the input in JavaScript: name将用于发布到服务器,但是id可用于引用JavaScript中的输入:

<input type='hidden' id='todo_0' name='todo[]'>
<input type='hidden' id='todo_1' name='todo[]'>

In JavaScript, document.getElementById("todo_0") will give you the first todo field. 在JavaScript中, document.getElementById("todo_0")将为您提供第一个todo字段。 Be sure to keep the id s sufficiently different that Internet Explorer doesn't get confused (it has namespace bugs around id and name [it tends -- completely incorrectly -- to put them in the same namespace]). 确保将id保持足够的不同,以使Internet Explorer不会混淆(它在idname周围存在名称空间错误[它倾向于- 完全不正确 -将它们放在相同的名称空间中])。

You could increment a counter in javascript as you add more fields, so you create todo[0], todo[1], etc. This wouldn't change how PHP interprets it. 随着添加更多字段,您可以在javascript中增加一个计数器,因此可以创建todo [0],todo [1]等。这不会改变PHP的解释方式。

Edit : Realised you aren't creating fields on the fly in javascript, but the naming still applies 编辑 :意识到您不会在javascript中即时创建字段,但命名仍然适用

You could give each of the todo inputs a unique ID that you remember, or, I believe you can use 您可以为每个待办事项输入提供一个您记得的唯一ID,或者,我相信您可以使用

<input type='hidden' name='todo[0]' />
<input type='hidden' name='todo[1]' />

etc. in your HTML. 等等。

If I understand what you are asking, you want to be able have to multiple fields that will be used to upload a file. 如果我了解您的要求,那么您希望能够具有多个用于上载文件的字段。 For example, if you have 3 files to modify, you would have three hidden todo fields? 例如,如果您有3个文件要修改,那么您将有3个隐藏的待办事项字段?

A quick and easy solution would be to keep a hidden field for the number of files such as: 一种快速简便的解决方案是为文件数量保留一个隐藏字段,例如:

<input type='hidden' name='numFiles' value='1' />

and update that as you add or remove files with javascript. 并在您使用javascript添加或删除文件时进行更新。 Then as others have suggested, give each todo a unique id as such: 然后像其他人所建议的那样,给每个待办事项一个唯一的ID,例如:

<input type='hidden' name='todo1' />

Now you can easily find a todo because each file will have a unique one and you will be able to update it from there. 现在,您可以轻松找到一个待办事项,因为每个文件都有一个唯一的待办事项,您可以从那里对其进行更新。

Once you post the form, you can pull the number of files there will be from the numFiles field and loop through all the todo's with a number appended to the end. 发布表单后,您可以从numFiles字段中提取文件数,并循环浏览所有待办事项,并在末尾附加一个数字。

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

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