隐藏时清除模态内容

时间:2018-10-10 05:03:58

标签: javascript jquery bootstrap-modal

我有此代码,不知道如何重置内容,在这种情况下为youtube。当关闭并打开另一个视频时,该视频相同。

public override int SaveChanges()
{
    foreach (var ent in ChangeTracker.Entries<Client>())
    {
        if (ent.State == EntityState.Modified)
        {
            // Get the changed values
            var modifiedProps = ObjectStateManager.GetObjectStateEntry(ent.EntityKey).GetModifiedProperties();
            var currentValues = ObjectStateManager.GetObjectStateEntry(ent.EntityKey).CurrentValues;
            foreach (var propName in modifiedProps)
            {
                var newValue = currentValues[propName];
                //log your changes
            }
        }
    }

    return base.SaveChanges();
}
$(document).ready(function() {
  /* Get iframe src attribute value i.e. YouTube video url
  and store it in a variable */
  var url = $("#cartoonVideo").attr('src');

  /* Assign empty url value to the iframe src attribute when
  modal hide, which stop the video playing */
  $("#myModal").on('hide.bs.modal', function() {
    $("#cartoonVideo").attr('src', '');
    // remove the bs.modal data attribute from it
    $(this).removeData('bs.modal');
    // and empty the modal-content element
    $('#modal-body .cartoonVideo').empty();
  });

  /* Assign the initially stored url back to the iframe src
  attribute when modal is displayed again */
  $("#myModal").on('show.bs.modal', function() {
    $("#cartoonVideo").attr('src', url);
  });
});

不起作用。有人可以帮助我吗? :(

1 个答案:

答案 0 :(得分:0)

您可以这样清除iframe:

     $("#myModal").on('hide.bs.modal', function() {   
           var iframe = document.getElementById("cartoonVideo");
           iframedoc =iframe.contentDocument || iframe.contentWindow.document;
           iframedoc.body.innerHTML = '';
      });

并在“ show.bs.modal”事件中设置新的“ src”

<a href="#myModal" class="xemvideo" data-toggle="modal" data-video="https://www.youtube.com/embed/pbQ2k4Q8y0o">XEM VIDEO 1</a>
<a href="#myModal" class="xemvideo" data-toggle="modal" data-video="https://www.youtube.com/embed/d46GppZFjbc">XEM VIDEO 2</a>    


 $("#myModal").on('show.bs.modal', function(e) {
        var btn = $(e.relatedTarget);
        var url= btn.data("video");
        $("#cartoonVideo").attr('src', url);
  });