javascript - jQuery append() not working inside a bootstrap modal - Stack Overflow

I have a bootstrap modal as following:<div class="modal fade" id="container-modal&quo

I have a bootstrap modal as following:

<div class="modal fade" id="container-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" data-backdrop="false">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        <ul class="modal_containers" id="list_ctns">
         <li><img src="images/container.png"></li>
         <li><img src="images/container.png"></li>
         <li><img src="images/container.png"></li>
        </ul>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

The above modal is triggered when a particular button is clicked which can be considered as a repository of certain elements.

I have a form which has a submit button. On clicking that submit button, I want to append a list item to the modal_body.

<div class="button-containers">
 <button class="btn btn-success" id="save_item" type="submit">Save</button>
</div>

So, I wrote this simple javascript snippet to append the list item to the modal.

$(document).ready(function(){
$('#save_item').click(function(e){
   $("#list_ctns").append("<li><img src="images/container.png"></li>")
});

});

I am not sure, where I am making a mistake. Please help me with your suggestions.

Cheers, SZ

I have a bootstrap modal as following:

<div class="modal fade" id="container-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" data-backdrop="false">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        <ul class="modal_containers" id="list_ctns">
         <li><img src="images/container.png"></li>
         <li><img src="images/container.png"></li>
         <li><img src="images/container.png"></li>
        </ul>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

The above modal is triggered when a particular button is clicked which can be considered as a repository of certain elements.

I have a form which has a submit button. On clicking that submit button, I want to append a list item to the modal_body.

<div class="button-containers">
 <button class="btn btn-success" id="save_item" type="submit">Save</button>
</div>

So, I wrote this simple javascript snippet to append the list item to the modal.

$(document).ready(function(){
$('#save_item').click(function(e){
   $("#list_ctns").append("<li><img src="images/container.png"></li>")
});

});

I am not sure, where I am making a mistake. Please help me with your suggestions.

Cheers, SZ

Share Improve this question asked Aug 3, 2015 at 18:25 ShellZeroShellZero 4,67112 gold badges42 silver badges59 bronze badges 1
  • if your button is of type submit then its going to cause a postback – Sushil Commented Aug 3, 2015 at 18:27
Add a ment  | 

2 Answers 2

Reset to default 4

Mind the quotes when you pass that as a string to set a value.

"<li><img src="images/container.png"></li>"

should be either

"<li><img src='images/container.png'></li>"

or

"<li><img src=\"images/container.png\"></li>"

Also don't forget to prevent the default action of the submit button.

$('#save_item').click(function(e) {
   e.preventDefault();
   $("#list_ctns").append("<li><img src='images/container.png'></li>")
});

The button type is submit so when you click it, the form will submit causing the page to be redirected to the "action" url.

You can create a JS function to handle it like this;

function handleModal(form)
{
    $("#list_ctns").append("<li><img src=\"images/container.png\"></li>");

    //Use $.AJAX() to submit the form without refreshing the page.

    return false; //this prevent the form from being submitted. Keep it if you are going to use $.AJAX() to submit the form or remove it if you want the form to submit normally.
}

and then call it from the <form> node by adding onsubmit="return handleModal(this);" attribute to it.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744941564a4602355.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信