将值传递给引导模式JavaScript和PHP

将值传递给引导模式JavaScript和PHP,javascript,php,jquery,twitter-bootstrap,modal-dialog,Javascript,Php,Jquery,Twitter Bootstrap,Modal Dialog,我试图在这里找到这个,但没有找到我想要的 目标:流程完成后,显示引导模式,通知用户该流程已完成(和/或管理员可能需要授权某些内容,例如,可能需要授权注释…) 我可以输出一个JavaScript脚本(键入该脚本时听起来很冗余),它将打开模式,但我想将文本传递给模式,这样我就可以拥有一个可重用的对话框。以下是我正在使用的基本代码: 情态形式: <!-- language: lang-html --> <!-- meant to be an alert di

我试图在这里找到这个,但没有找到我想要的

目标:流程完成后,显示引导模式,通知用户该流程已完成(和/或管理员可能需要授权某些内容,例如,可能需要授权注释…)

我可以输出一个JavaScript脚本(键入该脚本时听起来很冗余),它将打开模式,但我想将文本传递给模式,这样我就可以拥有一个可重用的对话框。以下是我正在使用的基本代码:

情态形式:

<!-- language: lang-html -->

            <!-- meant to be an alert dialog -->
            <div id="myAlert" class="modal fade" tabindex="-1" role="dialog">
               <div class="modal-dialog modal-lg" role="document">
                  <div class="modal-content">
                     <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h3 class="modal-title" id="descModalLabel"></h3>
                     </div> <!-- / modal-header -->
                     <div class="modal-body">
                        <!-- some text will be inserted here -->
                     </div><!-- / modal-body -->
                     <div class="modal-footer" -->
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                     </div><!-- / modal-footer -->
                  </div><!-- / modal-content -->
               </div><!-- /modal-dialog -->
            </div> <!--/ modal -->
            <!-- end of modal form -->

<!-- end snippet -->

&时代;
接近
打开表单的JavaScript:

// for the myAlert modal:
      // code to open the modal with the caption and description:
      $('#myAlert').on('show.bs.modal', function (event)
      {
         var modal = $(this);
         // need to get these values passed somehow
         var caption = "Test";
         var description = "Test Test Test";
         modal.find('.modal-title').text( caption );
         modal.find('.modal-body').append( '<strong>Description:</strong> ' + description );
      });
      
      // when modal closes, clear out the body:
      $('#myAlert').on('hidden.bs.modal', function ()
      {
          $(this).find(".modal-body").text('');
      });

//对于myAlert模式:
//打开带有标题和说明的模式的代码:
$('#myAlert').on('show.bs.modal',函数(事件)
{
var modal=$(本);
//需要以某种方式传递这些值
var caption=“测试”;
var description=“测试”;
modal.find('.modal title')。文本(标题);
modal.find(“.modal body”).append(“说明:”+说明);
});
//当modal关闭时,清除车身:
$('#myAlert').on('hidden.bs.modal',function()
{
$(this.find(“.modal body”).text(“”);

});可能有更好的解决方案,但这一个也可以,因为我已经测试过了。它需要采取3个步骤:

  • 在所有事件和函数之前,您需要声明要传递给JavaScript的变量(使它们成为全局变量)
  • 例如:

    var global_caption = "Some default caption";
    var global_description = "Some default description";
    
    // All scripts now go here
    
  • 在模式中,可以从全局变量(或直接使用全局变量)为某些事件指定局部变量,例如:
  • 这将显示到目前为止与您的案例基本相同的内容:

    $('#myAlert').on('show.bs.modal', function (event)
          {
             var modal = $(this);
             var caption = global_caption;
             var description = global_description;
             modal.find('.modal-title').text( caption );
             modal.find('.modal-body').append( '<strong>Description:</strong> ' + description );
          });
    

    我明白你的意思,但我不能让它发挥作用。我可能打错了什么东西,我要再看一遍。要测试代码php部分中的值:Oops过早地返回:echo'document.write(global_caption+“
    ”);回显“document.write(全局消息+”
    ”);回显“”;//如何传递变量???echo'global_caption=“Success!”;echo'global_message=“它起作用了!Test Testie Test””;echo'$(“#myAlert”).modal(“show”);回声';echo“document.write(全局标题+”
    ”);回显“document.write(全局消息+”
    ”);相同的值。您可以将此部分粘贴到其他位置吗?我很难读懂问题出在哪里。:)是的,格式被冲洗了。结果是我在引号内的值的回显赋值中漏掉了分号。。。我只是注意到没有,所以添加了它们,代码以我希望看到的值运行。非常感谢。我有些东西可以用。我仍然有兴趣看看是否有其他人有不同的方法,但这会有所帮助。嗯,我很高兴我能提供帮助。你能把我的回答标为接受吗?这将对我有很大帮助(直到其他人给出更好的答案)。谢谢
    echo '<script>';
    echo 'global_caption = "This is a new caption"';
    echo 'global_description = "This is a new description"';
    echo '$("#myAlert").modal("show")';
    echo '</script>';