简体   繁体   English

从PHP循环调用Javascript函数

[英]Call Javascript Function from PHP in a loop

I have been looking at answers to this across the web for about an hour now, and trying things based on those answers (I know this kind of issue comes up often). 大约一个小时以来,我一直在网上寻找有关此问题的答案,并根据这些答案尝试尝试(我知道这类问题经常会出现)。 However, nothing I am trying is working. 但是,我正在尝试的一切都没有。 So an explanation first: 因此,首先进行解释:

I am looping through a table in PHP (in a while loop), and need to call a JavaScript function for each record in the row set returned for my table. 我正在遍历PHP中的表(在while循环中),并且需要为表返回的行集中的每个记录调用JavaScript函数。 To try to get started, I have a super basic function that just lets me know I called it ... it's not working. 为了尝试入门,我有一个超级基本的功能,只是让我知道我叫它……这是行不通的。 Here's the PHP code (the reason for the DIV tag is that I eventually want to replace the innerHTML of the DIV from the JS code I know how to do that and have done so elsewhere ...): 这是PHP代码(使用DIV标签的原因是,我最终想从我知道怎么做的JS代码中替换DIV的innerHTML,并且已经在其他地方这样做了……):

// the loop:
while( $row = mysqli_fetch_array( $heralds_result ) )
{
   $herald_id = $row["roster_id"];
   $sca_name = $row["sca_name"];

   // create a div for each herald as we go with its' own name (using id ...)
   echo "      <div id='" . $herald_id . "'>\n";
   echo "         " . $sca_name . "\n";
   echo "         <script type='text/javascript'>test();</script>\n";
   echo "      </div>\n";

}

This is the JavaScript code (at the bottom of the file): 这是JavaScript代码(位于文件底部):

<script type="text/javascript">
    function test()
    {
       alert( "Test function" );
    }
</script>

However, I get no "alert" dialog, just the text streamed out. 但是,我没有“警告”对话框,只是文本流了出来。 So for the first row I get: 所以对于第一行,我得到:

  <div id='1'>
     Aasa Thorvaldsdoittr
     <script type='text/javascript'>test();</script>
  </div>

As the alert dialog is not displayed, it tells me the Javascript function is not being called, or there is some error that is truly not obvious to me after staring at it for a long time. 由于未显示警报对话框,因此它告诉我没有调用Javascript函数,或者长时间盯着它看后,有些错误对我来说确实是不明显的。 This is something that shouldn't be this difficult, I am sure I have some really obvious error that someone will see as soon as they look at the code, but it's evading me completely. 这不应该那么困难,我确信我有一个非常明显的错误,一旦有人查看代码,就会有人看到它,但这完全使我无所适从。

"JavaScript code at the bottom of the file" “文件底部的JavaScript代码”

is the problem. 是问题。 Scripts are run as soon as they are added to the DOM. 将脚本添加到DOM后立即运行。 Things are added to the DOM in the order they are given in the HTML. 事物按照它们在HTML中给出的顺序添加到DOM中。

Therefore the script tags which try to call your function are added - and executed - before the script tag which actually contains the function gets added. 因此,在添加实际包含该功能的脚本标签之前 ,将添加并执行尝试调用您的函数的脚本标签。 So they try to execute a function which doesn't yet exist. 因此,他们尝试执行尚不存在的功能。 If you look in your browser's Console (in the Developer Tools) you probably have some errors complaining about this. 如果您在浏览器的控制台(在开发人员工具中)中查看,则可能会有一些错误对此有所抱怨。

The simple solution is to declare the script tag containing the function somewhere in your HTML before the code which tries to use it. 一种简单的解决方案是在尝试使用该功能的代码之前 ,在HTML中的某个位置声明包含该功能的脚本标签。


As an aside, it would be interesting to know what you plan to do with this function in reality - consider whether you could achieve the same effect directly by creating some markup using PHP, rather than firing off lots of JS while the page is loading. 顺便说一句,知道您实际上打算使用此功能做什么是很有趣的-考虑您是否可以通过使用PHP创建一些标记直接实现相同的效果,而不是在页面加载时触发大量的JS。

You should move the function declaration upper than it used. 您应该将函数声明移到比使用的更高的位置。 For example: 例如:

<script type="text/javascript">
function test()
{
   alert( "Test function" );
}
</script>
..
<?php
..
// your loop
..
?>

or 要么

<?php
..
echo '<script type="text/javascript">
function test()
{
   alert( "Test function" );
}
</script>';
..
// your loop
..
?>

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

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