Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.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 如何增加append div的计数?_Javascript_Jquery - Fatal编程技术网

Javascript 如何增加append div的计数?

Javascript 如何增加append div的计数?,javascript,jquery,Javascript,Jquery,我在这件事上有很大的问题。我想增加计数,但没有增加。以我的div的名义,计数从3开始。输出将是:“faq[3][QUOTE]”,但问题是,它不会递增。我想发生的是,每次我添加新div时,它都会变成。常见问题[3][问题]、常见问题[4][问题],等等。我希望你们能帮助我 import app from '../app.setup'; app.dynamicField = class extends app.Component { onReady() { var wrapper =

我在这件事上有很大的问题。我想增加计数,但没有增加。以我的div的名义,计数从3开始。输出将是:“faq[3][QUOTE]”,但问题是,它不会递增。我想发生的是,每次我添加新div时,它都会变成。常见问题[3][问题]、常见问题[4][问题],等等。我希望你们能帮助我

import  app  from '../app.setup';

app.dynamicField = class extends app.Component {
onReady() {
    var wrapper = $(".container");
    var add_button = $(".btn-question");
    var count = 3;
    var _titleQuestion = 'faq["' + count + '"][question]';
    var _titleAnswer = 'faq["' + count + '"][answer]';

    $(add_button).click(function(e) {
        e.preventDefault();
            $(wrapper).append('<div><div class="form-group"> <input type="text" class="question form-control mb-3" name="' + _titleQuestion +'"  placeholder="Question"/><textarea class="form-control answer" name="' + _titleAnswer + '" placeholder="Answer"></textarea></div><a href="#" class="delete">Delete</a></div>');
            count++;
    });

    $(wrapper).on("click", ".delete", function(e) {
        e.preventDefault();
        $(this).parent('div').remove();
        count--;
    })
}
}
从“../app.setup”导入应用程序;
app.dynamicField=类扩展app.Component{
onReady(){
var包装器=$(“.container”);
var add_按钮=$(“.btn问题”);
var计数=3;
var_titleQuestion='faq[“+count+”][question];
var _titleAnswer='faq[“+count+”][答案];
$(添加按钮)。单击(功能(e){
e、 预防默认值();
$(包装器)。附加(“”);
计数++;
});
$(包装器)。在(“单击”,“删除”,函数(e){
e、 预防默认值();
$(this.parent('div').remove();
计数--;
})
}
}

单击处理程序函数对修改后的
计数
变量没有任何影响

您应该更新click事件处理程序函数中的变量:

..........
$(add_button).click(function(e) {
    var _titleQuestion = 'faq["' + count + '"][question]';
    var _titleAnswer = 'faq["' + count + '"][answer]';
    e.preventDefault();
    $(wrapper).append('<div><div class="form-group"> <input type="text" class="question form-control mb-3" name="' + _titleQuestion +'"  placeholder="Question"/><textarea class="form-control answer" name="' + _titleAnswer + '" placeholder="Answer"></textarea></div><a href="#" class="delete">Delete</a></div>');
    count++;
});

$(wrapper).on("click", ".delete", function(e) {
    var _titleQuestion = 'faq["' + count + '"][question]';
    var _titleAnswer = 'faq["' + count + '"][answer]';
    e.preventDefault();
    $(this).parent('div').remove();
    count--;
});
...........
。。。。。。。。。。
$(添加按钮)。单击(功能(e){
var_titleQuestion='faq[“+count+”][question];
var _titleAnswer='faq[“+count+”][答案];
e、 预防默认值();
$(包装器)。附加(“”);
计数++;
});
$(包装器)。在(“单击”,“删除”,函数(e){
var_titleQuestion='faq[“+count+”][question];
var _titleAnswer='faq[“+count+”][答案];
e、 预防默认值();
$(this.parent('div').remove();
计数--;
});
...........

请提供所有相关代码。我们不知道你的HTML是什么样子。我建议你阅读。@ErikPhilips我认为你不需要看html代码。我希望你能得到答案。我不会回答这个问题,因为没有HTML,我无法测试我的解决方案以满足您的需求。链接的存在是为了帮助您获得更多答案,因为许多人不想将代码反向工程为构建html(包括我),以便我们可以调试它。。。