繁体   English   中英

在php中回显jquery警报弹出窗口

[英]echoing a jquery alert popup in php

我希望在满足PHP的某些条件下显示弹出警告框。 就像是:

  echo "<script type="text/javascript"> alert('bleh'); </script>";

除了使用自定义jquery警报框。 这可能吗??

我尝试过类似的东西:

  echo "<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
   <link rel="stylesheet" href="/resources/demos/style.css" />
   <script>
   $(function() {
   $( "#dialog-message" ).dialog({
   modal: true,
   buttons: {
    Ok: function() {
      $( this ).dialog( "close" );
    }
  }
  });
});
</script>"; 

但它给了我一个奇怪的效果。 没弹出来。

谢谢你的关注。

echo <<<EOD
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <link rel="stylesheet" href="/resources/demos/style.css" />
    <script>
       $("#dialog-message").dialog({
           modal: true,
           buttons: {
               Ok: function() {
                  $( this ).dialog( "close" );
               }
           }
       });
    </script>
EOD;

尝试这样的事情。 $bleh是带有要显示的消息的PHP变量。

<script type="text/javascript">
$(document).ready(function(){
   var dlg = $('<div>').text(<?php echo $bleh ?>);
   $(body).append(dlg);
   dlg.dialog(
      modal: true
   );
});
<script>

PHP期望“rel =”stylesheet“等任何行中的”(双引号)都是php代码。你必须使用'(单引号)或者使用add_slashes这样的函数转义它们。为了解决这个问题,我通常在回显内容周围使用单引号,因此所有双引号都不会被打扰。

在这里回复“所有”引用的“东西”;

是的可能..

echo "<script type=\"text/javascript\">alert('bleh');</script>";

但我不确定它是否是一个在echo中编写整个函数的好方法,而是在HTML中定义你的函数,在页面中的某些位置,并像我上面那样回显函数名称。

如前所述,你需要逃避你的角色或像这样使用它,所以不需要在这里转义字符

<?php if (condition == true) : ?>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css" />
  <script>
   $(function() {
   $( "#dialog-message" ).dialog({
   modal: true,
   buttons: {
    Ok: function() {
      $( this ).dialog( "close" );
    }
  }
  });
 });
 </script>
<?php endif; ?>

当然可以。 你可能想要这个:

  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
   <link rel="stylesheet" href="/resources/demos/style.css" />
   //check for PHP condition. Script will only be output on condition pass.
   <?php if(condition == true){ ?>
   <script>
   $(function() {
    $("#dialog-message").dialog({
     modal: true,
     buttons: {
     Ok: function() {
        $(this).dialog("close");
      }
    }
   });
 });
 </script>
 <?php } ?>

基本上,只要您从.php文件而不是外部.js文件输出脚本,就不需要“回显”脚本。 只要条件通过,脚本就会显示出来。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM