Bootstrap modal เมื่อคลิกปุ่มต้องโหลดหน้าต่างโมดอลใหม่ทั้งหมด

Aug 18 2020

ฉันต้องการโหลดหน้าต่างโมดอลแบบเต็มเมื่อคลิกที่ปุ่ม (ขยายที่นี่) ตอนนี้การคลิกเป็นการเปิดหน้าต่างโมดอลและปิดหน้าต่างโมดอล

ถ้าฉันใช้รหัสนี้:

$('#youtubeModal').on('hidden.bs.modal', function (event) { $('#youtubeModal').modal('toggle')
})

มันเป็นเพียงการเปิดหน้าต่างโมดอลอีกครั้ง แต่ฉันต้องการโหลดใหม่ทั้งหมด

ตอนนี้ฉันมีความเข้มงวด:

  1. ฉันกดปุ่ม
  2. Modal แสดงด้วยการทำงานที่ดีทั้งหมด
  3. ฉันกดปุ่ม
  4. ซ่อน Modal
  5. ฉันกดปุ่ม
  6. Modal เปิดและโหลดอีกครั้ง ajax และทำงานได้ดี

ฉันต้องการลบ 4. และ 5. :

  1. ฉันกดปุ่ม

  2. Modal แสดงด้วยการทำงานที่ดีทั้งหมด

  3. ฉันกดปุ่ม

  4. Modal เปิดใหม่และโหลด ajax ใหม่และทำงานได้ดี

     <div class="modal fade" id="youtubeModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
         <div class="modal-dialog" role="document">
             <div class="modal-content">
                 <div class="modal-body">
                 </div>
                 <div class="modal-footer">
                     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                 </div>
             </div>
         </div>
     </div>
     <script>
         $('#youtubeModal').on('show.bs.modal', function (event) { let button = $(event.relatedTarget) // Button that triggered the modal
             let youtubeUrl = button.data('url') // Extract info from data-* attributes
    
             $.ajax({ url: '<?= \yii\helpers\Url::toRoute(['youtube/index'])?>', type: 'post', data: {youtubeUrl: youtubeUrl}, success(response) { // Add response in Modal body $('.modal-body').html(response);
                     //$('#exampleModal').html(response); // Display Modal //$('#exampleModal').modal('show');
                 }
             });
         });
     </script>
    

คำตอบ

ustmaestro Aug 18 2020 at 18:32

ดูเหมือนว่าคุณต้องมีทริกเกอร์ที่กำหนดเองและทำแบบเป็นโปรแกรมเนื่องจากทริกเกอร์สลับข้อมูล BS เริ่มต้นมีตรรกะอื่น

<span id="play-button" class="fa fa-play-circle play-button" data-url="watch?v=ik7ysDGWbcw" onclick="showYoutubeModal(this)">click me</span>


<div class="modal fade" id="youtubeModal" data-backdrop="false" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
    <div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-body">
        </div>
        <div class="modal-footer">
           <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
    </div>
    </div>
</div>
<script>
var youtubeModal = $('#youtubeModal').modal({'show': false}); youtubeModal.on('show.bs.modal', function (event) { let button = $(event.relatedTarget);
    let youtubeUrl = button.data('url');
    
    $.ajax({ url: '<?= \yii\helpers\Url::toRoute(['youtube/index'])?>', type: 'post', data: {youtubeUrl: youtubeUrl}, success(response) { // Add response in Modal body $('.modal-body').html(response);
        //$('#exampleModal').html(response); // Display Modal //$('#exampleModal').modal('show');
        }
    });
});

function showYoutubeModal(trigger) {
    // here you can call modal function programmatically
    // depending on your needs
    youtubeModal.modal('show', $(trigger));
}
</script>

คุณสามารถดูข้อมูลเพิ่มเติมได้ที่นี่

จะส่งผ่านพารามิเตอร์ไปยังโมดอลได้อย่างไรเมื่อฉันเรียกมันโดยใช้โปรแกรม