在同一页面弹出窗口中显示另一页面的模态内容

Similar questions like this has been posted before but none solved my issue. I'm trying to get enrollments.php page to show as a modal popup within index.php page

index.php

<button class="sess_btn sess_btn1" id="btn_enrol">Enrollments</button>  
<div id="enrolModal"></div>

<script>
$(document).ready(function() {
    //button modal
    $('#btn_enrol').click(function(){
        //using ajax post
        $.post('enrollments.php',function() {
            $('#enrolModal').html(); //Fill DIV enrolModal with enrollments.php content
        })
    //Calling Modal
    $('#myModal').modal('show');
    })
})
//-------------
</script>

enrollments.php

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <div class="modal-header">
      <span class="close">&times;</span>
      <h2>Modal Header</h2>
    </div>
    <div class="modal-body">
      <p>Some text in the Modal Body</p>
      <p>Some other text...</p>
    </div>
    <div class="modal-footer">
      <h3>Modal Footer</h3>
    </div>
  </div>

</div>

Also, when I try to click on the button several times there are quick blinks on the browser window. Any help will be appreciated, thanks.

dougu4448
dougu4448 关闭但不完全是我正在搜索的内容,一些代码示例会非常有用,因为我是AJAX的新手。
一年多之前 回复
donglong7338
donglong7338 另外,阅读AJAX请求的异步性质-确保在尝试操作DOM之前等待结果到达(将其转换为模态)。
一年多之前 回复
douchu5131
douchu5131 错误地使用$.post回调并错误地使用html()。从API文档$.post....html()开始,看一下示例
一年多之前 回复

2个回答

Thanks for the answer, I got it done a little differently. Instead of a button I used a link as shown.

index.html

<script>
    $(document).ready(function() {              
        $('.li-modal').on('click', function(e){
            e.preventDefault();
            $('#theModal').modal('show').find('.modal-content').load($(this).attr('href'));
        })
    });
</script>

    <a href="enrollments.php" class="li-modal" id="enrol_link" onMouseOver="this.style.background='rgb(0,66,79)'" onMouseOut="this.style.background='#4CAF50'">Enrollments</a>

    <div class="modal fade text-center" id="theModal">
        <div class="modal-dialog">
            <div class="modal-content">
            </div>
        </div>
    </div>   

enrollments.php

<script>
// Get the modal
var modal = document.getElementById('theModal');

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = "none";
    window.location.href = "updatesession.php";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
        window.location.href = "updatesession.php";
    }
}
</script>

    <div id="model-content">
        <div class="modal-header">
          <span id="close" class="close">&times;</span>
          <p>Session Editor</p>
        </div>
        <div class="modal-body">
          <p>Select Session: </p>
        </div>
        <div class="modal-footer">
          <p>Modal Footer</p>
        </div>
    </div>

Did the corrections in code to use the jquery API correctly. Not tested but, below code will work.

<script>
$(document).ready(function() {
    //button modal
    $('#btn_enrol').click(function(){

        $.post( "enrollments.php", function( htmlContents ) {
          $('#enrolModal').html( htmlContents ); 

          //Show the Modal once you get enrollments.php html contents
          $('#myModal').modal('show');
        });      
    })
})
//-------------
</script>
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问