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

Javascript 要仅在单击按钮时重置最后一个新插入的动态块吗

Javascript 要仅在单击按钮时重置最后一个新插入的动态块吗,javascript,jquery,html,Javascript,Jquery,Html,var qBlock=''; $addQuestion.onclick,函数{ $qBlock.insertAfter.qandaBlock; }; $resetblock.onclick,'.closequest',函数{ 你真的想删除这个问题吗?!?; $this.closest.newqandaBlock.remove; }; $resetblock.onclick,'.resetlatest',函数{ 你真的想重新设置这个问题吗?!?; $this.closest'form'.findi

var qBlock=''; $addQuestion.onclick,函数{ $qBlock.insertAfter.qandaBlock; }; $resetblock.onclick,'.closequest',函数{ 你真的想删除这个问题吗?!?; $this.closest.newqandaBlock.remove; }; $resetblock.onclick,'.resetlatest',函数{ 你真的想重新设置这个问题吗?!?; $this.closest'form'.findinput[type=text],textarea.val; }; 大概是这样的:

为了获得最后或新插入的元素,我使用jquery的:last-child选择器,如下所示

如果您有一个模板,那么使用.clone也比使用javascript构建html更好,更易于维护

<div class="content">
    <div id="template" class="section">
        <input class="question" type="text" />
        <br/>
        <textarea class="answer"></textarea>
        <br/>
        <textarea class="comments"></textarea>
    </div>
</div>
<br/>
<br/>
<a href="#" class="reset" style="margin-right:10px;">Reset Last Question</a>
<a href="#" class="add">Add Another Question</a>

$('.add').on('click', function() {
    var clone = $('#template').clone(true).attr('id', '');
    clone.find('.question').val('');
    clone.find('.answer').val('');
    clone.find('.comments').val('');
    clone.appendTo('.content');
});

$('.reset').on('click', function() {
    console.log($('.section').length);
    var last = $('.content .section:last-child');
    last.find('.question').val('');
    last.find('.answer').val('');
    last.find('.comments').val('');

});

除了sh**ty标记之外,还有一些需要更改的内容:

1停止表单相互嵌套,将新表单放在其他表单之后的底部:

$("#addQuestion").on("click",function(){        
     $(qBlock).insertAfter(".qandaBlock");
});

2从标记和JS中删除所有id=resetblock

3将所有$resetblock.onclick更改为$document.onclick

4将警报更改为确认:


您正在复制ID。ID在HTML文档中必须是唯一的。根据您的代码,您将在每次迭代/单击中嵌套。另外,您的代码片段似乎不起作用。请检查1是否实际加载了jQuery,2是否在DOM ready上添加了事件处理程序,3是否在控制台中有任何错误消息。现在检查@Terry,有人编辑了该代码片段,我发布了什么。现在它正在运行。但我只想重置最后一个新插入的块的字段,以便在单击“重置”时清除,而不是全部重置或不重置之前的字段。@Pooja您所做的只是将其回滚到您糟糕的标记中。编辑只是整理了你的代码,让它可以远程读取。Terry的评论仍然有效,您的代码插入了一个重复的ID,而不仅仅是将它嵌套在另一个ID中。
$("#addQuestion").on("click",function(){        
     $('body').append(qBlock); // Append the form to the body, so it will sit after the last form
});
var blnReset = confirm("Do you want to reset the question?");
if (blnReset == true) {
   //Reset the question
}