我想在每次单击按钮添加地址时使用jQuery
<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">
$("#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>
答案 0 :(得分:4)
您必须将您的jquery代码括在$(document).ready()块中,如下所示: -
$(document).ready(function(){
//Jquery code here
});
OR
$(document).on('click', '#btnAddAddress', function()
{...});
最后不要忘记在页面上添加jquery库文件。
注意: - 通常应该使用第二个答案,以防我们动态地将HTML添加到DOM,否则使用第一个答案。
答案 1 :(得分:1)
如果在按钮之后写入js代码,则您提供的代码将不进行任何修改。 即。
<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>
&#13;
确保在执行代码之前加载了按钮和jquery。最佳选择由&#34; Kartikeya&#34;。
提供答案 2 :(得分:0)
你需要添加“return false;”在点击功能结束时。
$("#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;
});
答案 3 :(得分:0)
您的代码工作正常here。我也简化了你的代码,如下所示。
$("#btnAddAddress").click(function () {
$("#container").append($(".address").html());
});