简体   繁体   English

jQuery克隆功能不起作用

[英]Jquery Clone Function Not Working

I am trying to duplicate a div while clicking on a button. 我试图在单击按钮时复制div。 But when i click on the button the div appears, page refreshes and div removed. 但是,当我单击按钮时,将显示div,页面将刷新并删除div。 Here is my jscript code: 这是我的jscript代码:

$("#button").click(function () {

        $('.duplicater').clone().insertAfter(".duplicater");

    });

Here is my html code 这是我的HTML代码

<div id="duplicater" class="duplicater">
The text goes here
</div>

<button id="button" class="btn btn-info btn-xs">Add More</button>

i want if the user clicks on the button the div appears without refreshing page... 我想如果用户单击按钮,则div出现而不刷新页面...

button acts as submit due to which it might be doing page submit, so you could try changing your button tag, from: 由于按钮可能会进行页面提交,因此它充当了提交的角色,因此您可以尝试从以下位置更改按钮标签:

<button id="button" class="btn btn-info btn-xs">Add More</button>

to

<input type="button" id="button" class="btn btn-info btn-xs" value="Add More" />

if none specified the type of button tag is submit by default, so you could also try adding type="button" as: 如果没有指定按钮标签的类型默认情况下是submit ,那么您还可以尝试将type =“ button”添加为:

<button type="button" id="button" class="btn btn-info btn-xs">Add More</button>

Just checked your code on fiddle Fiddle 只是检查你的代码上拨弄拨弄

Its seems working fine . 它似乎工作正常。 Incase the the javascript is loaded before loading the button element please keep the click event inside $(document).ready(handler); 如果在加载button元素之前加载了javascript,请将click事件保留在$(document).ready(handler);中。

$(document).ready(function(){
    $("#button").click(function () {

        $('.duplicater').clone().insertAfter(".duplicater");

    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM