简体   繁体   English

为什么jQuery replaceWith元素克隆不适用于Twitter BootStrap模态?

[英]Why is jQuery replaceWith element clone not working with Twitter BootStrap modals?

I have a bootstrap modal whose body contains a span element. 我有一个引导程序模态,其主体包含一个span元素。 When a link is clicked, that link's data-user-name attribute is inserted into the span element inside the modal and the modal is shown. 单击链接后,该链接的data-user-name属性将插入到模式内部的span元素中,并显示模式。 When the proceed button is clicked, a status message is inserted into the modal body. 单击继续按钮后,状态消息将插入到模态主体中。 when the close button is clicked, the modal is closed, but the modal needs to return to it's original state when the page was loaded. 当单击关闭按钮时,模态被关闭,但是模态需要在加载页面时返回到其原始状态。 Here is what I have so far: 这是我到目前为止的内容:

<a href="#" data-username="Olivia Benson"><i class="fa fa-plus"></i></a>
<a href="#" data-username="Elliot Stabler"><i class="fa fa-plus"></i></a>
<a href="#" data-username="John Munch"><i class="fa fa-plus"></i></a>

<div class="modal fade" id="userAccess" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Grant Access to <?php echo get_the_title($_GET['id']);?></h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <p>You are about to grant <span id="userDisplayName"></span> access to manage <span><?php echo get_the_title($_GET['id']);?>.</span></p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" id="proceed">Proceed</button>
        <button type="button" class="btn btn-secondary" data-dismiss="modal" id="close">Close</button>
      </div>
    </div>
  </div>
</div>

(function($){
    $modalClone = $('.modal#userAccess').clone();
    $('#userAccess').on('hide.bs.modal', function(){
        $('.modal#userAccess').replaceWith($modalClone);
    });
    $('a[data-username']').on('click', function(e){
        $username = $(this).attr('data-username');
        $('.modal#userAccess span#userDisplayName').html($username);
        $('.modal #userAccess #close').on('click', function(){
            $('.modal#userAccess').modal('hide')
        $('#proceed').on('click', function(){
            $('.modal#userAccess .modal-body').html(
                $('<p></p>').html("You have granted "+$username+" access.")
        });
        e.preventDefault();
    });
})(jQuery);

The problem is that the modal is never replaced with the clone of the original. 问题在于,模态永远不会被原始模型的克隆替代。 What am I doing wrong? 我究竟做错了什么? There are no errors in console. 控制台中没有错误。

Here is a jsFiddle to demonstrate. 这是一个jsFiddle进行演示。 Click on a link. 点击一个链接。 Click proceed. 单击继续。 The content of the modal body will change. 模态主体的内容将改变。 Then close the modal and click on a different link. 然后关闭模态并单击其他链接。 The status message that was inserted when the proceed button was clicked the first time will still be there. 第一次单击“继续”按钮时插入的状态消息仍然存在。 It should display the message that was displayed the first time you opened the modal, but it doesn't. 应该显示第一次打开模态时显示的消息,但不会显示。 The procedures might work the first time you go through them, but they won't work after that. 该过程可能在您初次通过时起作用,但之后将不起作用。

You had two problems here. 您在这里遇到了两个问题。 First when you listen for hide.bs.modal your target was the modal that was on DOM at that moment. 首先,当您侦听hide.bs.modal您的目标就是当时在DOM上的模式。 But later you replace with it clone, this listener doesn't know about newly added modal. 但是稍后您将其替换为克隆,此侦听器将不了解新添加的模态。 To solve this you need to listen to document, because it is static and the event eventually will bubble up to it. 为了解决这个问题,您需要监听文档,因为它是静态的,事件最终会冒泡到它。 The second problem is you only have one clone, and after replacing the old one with that clone, you don't have fresh clone, but replacing same element. 第二个问题是您只有一个克隆,并且用该克隆替换了旧的克隆之后,您没有新的克隆,而是替换了相同的元素。 That's why you need to clone cloned element itself when replacing. 因此,替换时需要克隆克隆的元素本身。 This way you'll always have fresh modal instance. 这样,您将始终拥有新鲜的模态实例。

(function($){
    $modalClone = $('.modal#userAccess').clone();
    $(document).on('hide.bs.modal', '.modal#userAccess', function(){ // here 
          console.log($modalClone.clone())
        $('#userAccess').replaceWith($modalClone.clone()); // and here
    });
    $('a[data-username]').on('click', function(e){
        $username = $(this).attr('data-username');    
        $('.modal#userAccess span#userDisplayName').html($username);
        $('#userAccess').modal('show')
        $('.modal #userAccess #close').on('click', function(){
            $('.modal#userAccess').modal('hide')
        });
        $('#proceed').on('click', function(){
          $('.modal#userAccess .modal-body').html(
            $('<p></p>').html("You have granted "+$username+" access.")
          )
            $(this).remove()
        })
        e.preventDefault();
    });
})(jQuery);

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

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