Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.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_Html_Arrays_Multidimensional Array - Fatal编程技术网

Javascript 按下按钮增加数据量,然后按下多维数组

Javascript 按下按钮增加数据量,然后按下多维数组,javascript,jquery,html,arrays,multidimensional-array,Javascript,Jquery,Html,Arrays,Multidimensional Array,我有一个问题,当我按下按钮时,它会添加另一个元素,加上元素的名称在数组中也会很好地增加,有点难以解释,但我会提供一些屏幕截图 我在这个网站上搜索了很多解决方案,不幸的是,我似乎无法将其应用到我的问题上 以下是我的欲望输出的截图 如果我按下“添加”按钮,另一个字段集将显示在其上方 结果将是 因此,如果我再次按下“添加”按钮,它将显示另一个永无止境的字段集循环,并显示相应的递增输入类型名称 代码: 采区布局 <fieldset style="border:1px solid #DSDCDF

我有一个问题,当我按下按钮时,它会添加另一个元素,加上元素的名称在数组中也会很好地增加,有点难以解释,但我会提供一些屏幕截图

我在这个网站上搜索了很多解决方案,不幸的是,我似乎无法将其应用到我的问题上

以下是我的欲望输出的截图

如果我按下“添加”按钮,另一个字段集将显示在其上方

结果将是

因此,如果我再次按下“添加”按钮,它将显示另一个永无止境的字段集循环,并显示相应的递增输入类型名称

代码:

采区布局

<fieldset style="border:1px solid #DSDCDF; padding:10px; margin-bottom:10px;" id="fieldset_fblikeus">
    <div class="condition">
        <div class="clear"></div>
        <div>Label</div>
        <div><input class="gate-condition" name="label_" size="30" type="text" value="<?php echo $fb_page[0]; ?>"></div>
        <div class="clear" style="padding-top:5px;"></div>
        <div>URL to Like</div>
        <div><input class="gate-condition" name="url_" size="30" type="text" value="<?php echo $fb_page[1]; ?>"></div>
        <div class="clear" style="padding-top:5px;"></div>
    </div>
</fieldset>
这很难解释,还是希望你们能理解我的问题。
如果你们能帮我我会很高兴的,这很重要。提前感谢。

解决方法之一是在包含要插入的标记的页面上的某个位置有一个隐藏的“template”div。在这种特定情况下,标记相对简单,因此您可以将模板标记存储为JavaScript字符串,但随着时间的推移,这通常比在页面上使用隐藏的div更难维护

在任何情况下,一旦您有了模板标记,您只需要添加少量JavaScript代码来跟踪您向页面添加新的
字段集的次数。有了它,您可以更新
fb_likeus_add()
函数,在模板标记上执行简单的字符串替换,在将其预写到div之前插入正确的序列号

这听起来可能有点复杂,但实际上只需要很少的代码:

//initialize this when the page loads
var numAdded = 0;

function fb_likeus_add() {
    //increment the counter
    numAdded++;  

    //grab the template html
    var templateHtml = $("#template").html();  

    //write the new count into the template html
    templateHtml = templateHtml.replace(/label_/g, "label_" + numAdded)
                               .replace(/url_/g, "url_" + numAdded);  

    //prepend the updated HTML to the document  
    $("#container").prepend(templateHtml);  
};

这里有一个例子:

你是说,像这样的事情?对那个!谢谢!,你应该把它写在答案上,这样我才能接受。不过,谢谢,我欠你一个人情。
Array = [

div1 ['name_1', 'url_1'],

div2 ['name_2', 'url_2'],

div3 ['name_3', 'url_3'],

div4 ['name_4', 'url_4'],

div5 ['name_5', 'url_5']

and so on, it increments when you click the add button.

];
//initialize this when the page loads
var numAdded = 0;

function fb_likeus_add() {
    //increment the counter
    numAdded++;  

    //grab the template html
    var templateHtml = $("#template").html();  

    //write the new count into the template html
    templateHtml = templateHtml.replace(/label_/g, "label_" + numAdded)
                               .replace(/url_/g, "url_" + numAdded);  

    //prepend the updated HTML to the document  
    $("#container").prepend(templateHtml);  
};