Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript:创建具有不同名称的表单字段?_Javascript_Jquery - Fatal编程技术网

JavaScript:创建具有不同名称的表单字段?

JavaScript:创建具有不同名称的表单字段?,javascript,jquery,Javascript,Jquery,我使用下面的代码创建文件上传输入字段,它工作得非常好 但是,我需要这样重命名它们: mytext[1] mytext[2] mytext[3] 等等等等 所以我只是简单地把var I=0

我使用下面的代码创建文件上传输入字段,它工作得非常好

但是,我需要这样重命名它们:

mytext[1]
mytext[2]
mytext[3]
等等等等

所以我只是简单地把
var I=0I++
放在代码中。当我提醒
I
时,我在提醒框中得到1,2,3等,因此我知道这是可行的

但是这一行是错误的,我不知道如何在这一行中使用
I
变量:

<div><input type="file" name="mytext[i]"/></div>

这是全部代码:

<script>
$(document).ready(function() {
    var max_fields      = 10; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID
    var i = 0;

    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            i++
            $(wrapper).append('<div><input type="file" name="mytext[[i]]"/></div>'); //add input box
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});
</script> 

$(文档).ready(函数(){
var max_fields=10;//允许的最大输入框数
var wrapper=$(“.input_fields_wrapp”);//字段包装器
var add_button=$(“.add_字段_button”);//添加按钮ID
var i=0;
var x=1;//初始文本框计数
$(添加按钮)。单击(函数(e){//在添加输入按钮上单击
e、 预防默认值();
如果(x
和HTML代码:

            <div class="input_fields_wrap">
    <button class="add_field_button">Add More Photos</button>

    <div><input type="file"  name="mytext[]"/></div>
</div>

添加更多照片
有人能就此提出建议吗


谢谢

您需要将值连接到字符串中,您不能只在实际字符串值中添加
i

另外,我不知道为什么您有双方括号,我想您只需要一套

试试这个:

$(wrapper).append('<div><input type="file" name="mytext[' + i + ']"/></div>');
$(包装器).append(“”);

试试这样的方法

$(wrapper).append("<div><input type='file' name='mytext[["+i+"]]'/></div>");
$(包装器)。追加(“”);

您甚至不需要添加“i”。PHP将在您提交论坛时自动执行此操作。除非你想给他们起别的名字。代码越短,维护就越容易。

您如何知道OP使用的是PHP?