[英]Add div dynamically on button click using jQuery
I want to add div structure everytime I click the button to add an address using jQuery
我想每次单击按钮以使用
jQuery
添加地址时添加div结构
<div id="container">
<div class="address">
<div class="input-reg rb-item input-group">
<span class="input-group-addon">Address </span>
<input type="text" class="form-control" placeholder="Insert text here">
</div>
<div align="center"><iframe class="map-img" width="100%" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?hl=en&ie=UTF8&ll=37.0625,-95.677068&spn=56.506174,79.013672&t=m&z=4&output=embed"></iframe></div>
</div>
</div>
<div align="center" style="margin-top: 10px"><button id="btnAddAddress" class="btn btn-warning btn-md" type="button">Add Address</button></div>
and this is what I've tried but not working 这是我尝试过但无法正常工作的
<script type="text/javascript">
$("#btnAddAddress").click(function () {
$("#container").append('<div class="address"><div class="input-reg rb-item input-group"><span class="input-group-addon">Address </span><input type="text" class="form-control" placeholder="Insert text here"></div><div align="center"><iframe class="map-img" width="100%" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?hl=en&ie=UTF8&ll=37.0625,-95.677068&spn=56.506174,79.013672&t=m&z=4&output=embed"></iframe></div></div>');
});
</script>
You have to enclose your jquery code inside $(document).ready() block as shown :- 您必须将jquery代码封装在$(document).ready()块中,如下所示:
$(document).ready(function(){
//Jquery code here
});
OR 要么
$(document).on('click', '#btnAddAddress', function()
{...});
and lastly don't forget to add jquery library file on your page. 最后,不要忘记在页面上添加jquery库文件。
Note :- Second answer should normally be used in case when we dynamically add html to the DOM otherwise use first answer. 注意:-当我们将html动态添加到DOM时,通常应使用第二个答案,否则使用第一个答案。
The code you provided will work with out any modifications, if the js code is written after the button. 如果将js代码写在按钮后面,则您提供的代码将可以进行任何修改。 ie
即
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="container"> <div class="address"> <div class="input-reg rb-item input-group"> <span class="input-group-addon">Address </span> <input type="text" class="form-control" placeholder="Insert text here"> </div> <div align="center"><iframe class="map-img" width="100%" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?hl=en&ie=UTF8&ll=37.0625,-95.677068&spn=56.506174,79.013672&t=m&z=4&output=embed"></iframe></div> </div> </div> <div align="center" style="margin-top: 10px"><button id="btnAddAddress" class="btn btn-warning btn-md" type="button">Add Address</button></div> <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script> <script type="text/javascript"> $("#btnAddAddress").click(function () { $("#container").append('<div class="address"><div class="input-reg rb-item input-group"><span class="input-group-addon">Address </span><input type="text" class="form-control" placeholder="Insert text here"></div><div align="center"><iframe class="map-img" width="100%" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?hl=en&ie=UTF8&ll=37.0625,-95.677068&spn=56.506174,79.013672&t=m&z=4&output=embed"></iframe></div></div>'); }); </script>
Make sure that the button and jquery is loaded before your code is executed. 在执行代码之前,请确保已加载按钮和jquery。 The best option is provided by "Kartikeya".
最好的选择是“ Kartikeya”。
You need to add "return false;" 您需要添加“ return false;”。 at the end of click function.
在点击功能结束时。
$("#btnAddAddress").click(function () {
$("#container").append('<div class="address"><div class="input-reg rb-item input-group"><span class="input-group-addon">Address </span><input type="text" class="form-control" placeholder="Insert text here"></div><div align="center"><iframe class="map-img" width="100%" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?hl=en&ie=UTF8&ll=37.0625,-95.677068&spn=56.506174,79.013672&t=m&z=4&output=embed"></iframe></div></div>');
return false;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.