click中的pendig引导模式用于加载远程数据

pendig bootstrap modal in click for load remote data

本文关键字:程数据 数据 加载 用于 pendig 中的 模式 click      更新时间:2023-09-26

我有这样的代码,用于使用jquery ajax将动态数据从远程文件加载到引导程序中:

JS:

$(function(){
   $('.push').click(function(){
      var essay_id = $(this).attr('id');
        $.ajax({
        type : 'post',
        url : 'your_url.php', // in here you should put your query 
        data :  'post_id='+ essay_id, // here you pass your id via ajax .
                     // in php you should use $_POST['post_id'] to get this value 
       success : function(r)
           {
              // now you can show output in your modal 
              $('#mymodal').show();  // put your modal id 
             $('.something').show().html(r);
           }
    });
 });
});

HTML:

<a href="#" id="1" class="push">click</a> 
<div class="modal-body">  
   <div class="something" style="display:none;">
     // here you can show your output dynamically 
   </div>
</div>

这对我有效,但模式框在加载/挂起数据之前不会显示。我需要在点击后加载模态框,然后加载数据。

怎么解决这个问题?!

您可以在加载时在点击事件之外进行ajax调用,并在点击时显示隐藏:

$(document).ready(function() {
var essay_id = $(this).attr('id'); 
var results;
$.ajax({ 
      type : 'post', 
      url :  'your_url.php',
      async: false
      'post_id='+ essay_id, 
      success : function(r) { 
          results = r;
      } 
 });
$(' your_element').click(function() {
      if (results) {
          $('#mymodal').show(); 
          $('.something').show().html(results); 
      } 
 });
 });

使用async:false将强制它在继续脚本之前完成您的请求。希望这能有所帮助。