使用jQuery添加/删除div

时间:2017-02-03 10:24:30

标签: javascript jquery

我是JavaScript的新手,我有一个基本的设置,我可以用另一个div附加一个div。我正在尝试创建一个按钮,让我删除动态添加的div,这些是两个函数:

$("#add_new").click(function(){
event.preventDefault();
clicks += 1;
if (clicks < 10)
{
    //document.getElementById("total_steps_count").innerHTML = clicks;
    $("#steps").append('<div id="remove"><input type="text" id="step_title" class="form-control" name="step_title[]" placeholder="Enter a title for this step"><br><input type="file" id="step_image" name="step_image[]" ><br><input type="text" class="form-control" id="step_caption" name="image_caption[]" placeholder="Enter a caption for this image"><br><input type="text" id="step_text" name="step_text[]" class="form-control" placeholder="Walk us through this step"><br><button class="btn btn-default" id="remove_step"><i class="fa fa-times"></i> Remove This Step</button></div><br><br><hr>')
}
else
{
    alert("The maximum number of allowed steps is 10");
}
});



//Remove step
$("body").on("click", "#remove_story_step", function(){
    event.preventDefault();
    clicks -= 1;
    $(this).closest("#remove_story").remove()
});

当&quot; add_new&#39;单击按钮,div被正确附加到目标div,但是当我点击“删除”步骤时,按钮,页面刷新,所有以前添加的div都消失了。

就像我说我是JavaScript / jQuery的新手所以我不确定如何调试它,任何帮助都将不胜感激!

0 个答案:

没有答案
相关问题