问题很简单,我尝试将值推入数组,但是每次创建新值时都会创建,而不是添加,我尝试了多种方法来做同样的事情,您可以在此处检查代码我实际上在做什么
$(function(){
// Append Theme Pages
let counter = 2;
$(".js-append-pages").on("click", function(){
$(".js-pages-append-area").append(
`<div class="input-field col xs12 s6">
<input name="page_${counter}" id="page_${counter}" type="text" class="validate">
<label for="page_${counter}">Page Name</label>
</div>
<div class="file-field input-field col xs12 s6">
<div class="btn">
<span>Image</span>
<input type="file">
</div>
<div class="file-path-wrapper">
<input name="page_${counter}_img" class="file-path validate" type="text">
</div>
</div>`);
let inputArr = ["page_1"];
inputArr.push(`page_${counter}`);
console.log(inputArr);
counter++;
});
})
答案 0 :(得分:0)
$(function (){
let counter = 1;
let inputArr = ["page_1"];
$(".js-append-pages").on("click", function ()
{
$(".js-pages-append-area").append(
`<div class="input-field col xs12 s6">
<input name="page_${counter}" id="page_${counter}" type="text" class="validate">
<label for="page_${counter}">Page Name</label>
</div>
<div class="file-field input-field col xs12 s6">
<div class="btn">
<span>Image</span>
<input type="file">
</div>
<div class="file-path-wrapper">
<input name="page_${counter}_img" class="file-path validate" type="text">
</div>
</div>`);
inputArr.push(`page_${++counter}`);
console.log(inputArr);
});
});
这应该是实现它的更好方法。