最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change bootstrap modal title created on the fly - Stack Overflow

programmeradmin4浏览0评论

I have a bootstrap modal created on the fly like this

var popupTemplate = '
<div class="modal fade">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title" id="dropzoneModalTitle"></h4>
      </div>
      <div class="modal-body dropBox" id="dropzoneModalBody"></div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" data-dismiss="modal">Save</button>
        <button type="button" class="btn btn-link" data-dismiss="modal">Cancel</button>
      </div>
    </div>
  </div>
</div>
';

then with a onClick listener with jquery i create a modal with that template like this

$(button).click(function(){
   $(popupTemplate).modal()
});

i want to change the title of that modal created on the fly, but is not working with this

var modal = $(popupTemplate).modal();
modal.find('.modal-title').text('HELLO');

any help for this?

I have a bootstrap modal created on the fly like this

var popupTemplate = '
<div class="modal fade">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title" id="dropzoneModalTitle"></h4>
      </div>
      <div class="modal-body dropBox" id="dropzoneModalBody"></div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" data-dismiss="modal">Save</button>
        <button type="button" class="btn btn-link" data-dismiss="modal">Cancel</button>
      </div>
    </div>
  </div>
</div>
';

then with a onClick listener with jquery i create a modal with that template like this

$(button).click(function(){
   $(popupTemplate).modal()
});

i want to change the title of that modal created on the fly, but is not working with this

var modal = $(popupTemplate).modal();
modal.find('.modal-title').text('HELLO');

any help for this?

Share Improve this question edited Jul 6, 2018 at 0:19 user7637745 9852 gold badges14 silver badges27 bronze badges asked Jul 5, 2018 at 22:57 Dante CervantesDante Cervantes 3131 gold badge3 silver badges14 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

Try modifying the HTML before creating the modal:

var modal = $(popupTemplate);
modal.find('.modal-title').text('HELLO');
modal.modal();

Another way is to change it inside the show event. Just add an id attr to the root div and do the following:

 $('#modalId').on('show.bs.modal', function () {

        $('#dropzoneModalTitle').text('My New Modal Title');

  });
发布评论

评论列表(0)

  1. 暂无评论