猿问

数组推送功能无法正常工作

问题很简单,我正在尝试将值推送到数组中,但它每次都会创建新的值而不是添加我尝试了很多方法来做同样的事情你可以在这里检查代码我实际上在做什么


$(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++;

 });


})


大话西游666
浏览 143回答 1
1回答

海绵宝宝撒

&nbsp; $(function (){&nbsp; &nbsp; let counter = 1;&nbsp; &nbsp; let inputArr = ["page_1"];&nbsp; &nbsp; $(".js-append-pages").on("click", function ()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $(".js-pages-append-area").append(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `<div class="input-field col xs12 s6">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input name="page_${counter}" id="page_${counter}" type="text" class="validate">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label for="page_${counter}">Page Name</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="file-field input-field col xs12 s6">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="btn">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span>Image</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="file">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="file-path-wrapper">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input name="page_${counter}_img" class="file-path validate" type="text">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>`);&nbsp; &nbsp; &nbsp; &nbsp; inputArr.push(`page_${++counter}`);&nbsp; &nbsp; &nbsp; &nbsp; console.log(inputArr);&nbsp; &nbsp; });});这应该是一个更好的实现方式。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答