简体   繁体   English

从php循环调用Java脚本函数

[英]calling java script function from php loop

i am wounding if i can do this, as i am trying this from last 2 days but no success. 如果我能做到,我会很受伤,因为我从最近两天开始尝试,但是没有成功。

i want to run java script function from php 我想从PHP运行Java脚本功能

like 喜欢

php example PHP的例子

<div id="meprocess"></div>
<?php
for($i=1;$i<=10;$i++)
{
?>
<script>
jfun(<?php echo $i;?>);
</script>
<?php
}
?>

java script Java脚本

<script type="text/javascript">

function jfun(n)
{
html="Loop is on" + n;
$("<div>"+ html +"</div>").appendTo("div#meprocess");   
}
</script>

i am trying to generate a page which print loop number one by one.... 我正在尝试生成一页循环打印第一页的...。

but i got error jfun is not defined. 但我得到了错误jfun未定义。

what i can understand my be java script function is not ready at the time of php execution. 我能理解的是我的Java脚本功能在php执行时还没有准备好。 is there any way round, like running the php loop when DOM/Page is ready so that java script function will be ready that time, or any other way round.... 是否有任何方法,例如在DOM / Page准备就绪时运行php循环,以便Java脚本函数在该时间准备就绪,或者其他方法。

Thanks 谢谢

You have to define that function before calling it 您必须在调用之前定义该函数

http://sandbox.phpcode.eu/g/15b02.php http://sandbox.phpcode.eu/g/15b02.php

<div id="meprocess"></div>
<script type="text/javascript">

function jfun(n)
{
html="Loop is on" + n;
$("<div>"+ html +"</div>").appendTo("div#meprocess");   
}
<?php
for($i=1;$i<=10;$i++)
{
?>
jfun(<?php echo $i;?>);
<?php
}
?>
</script>

Your PHP code is generating (in a very bad way..) some javascript code that will be executed by the script. 您的PHP代码正在生成(以一种非常糟糕的方式..)一些将由脚本执行的javascript代码。 Although it should work, this is definitely not the best way to do that - you should run the loop on the js-side, eventually passing values from your PHP script.. 尽管它应该起作用,但这绝对不是最好的方法-您应该在js端运行循环,最终从PHP脚本传递值。

However: 然而:

The "undefined function" problem is given since you try to run the code before the function is created; 由于您尝试在创建函数之前运行代码,因此出现了“未定义的函数”问题。 using jQuery, to make some code executed only after everything is loaded, use: 使用jQuery,要使某些代码仅在所有内容加载后才执行,请使用:

$(document).ready(function(){
// Your code here..
})

or just 要不就

$(function(){
// Your code here..
})

As described here: http://api.jquery.com/ready/ 如此处所述: http : //api.jquery.com/ready/

For the for loop, you should generate some js like this: 对于for循环,您应该生成如下这样的js:

<div id="meprocess"></div>
<script type="text/javascript">
for (i=1; i<=10; i++) {
jfun(i);
}
</script>

..maybe, generating the min/max values from the PHP script: ..也许从PHP脚本生成最小值/最大值:

<?php
  $min = 1;
  $max = 10;
?>
<div id="meprocess"></div>
<script type="text/javascript">
for (i=<?php print $min; ?>; i<=<?php print $max; ?>; i++) {
jfun(i);
}
</script>

..that's more good practice than writing N function calls in order to loop.. ..比循环编写N个函数调用更好的做法。

First of all, ensure that your javascript function is above function uses. 首先,请确保您的javascript函数在函数使用之上。

Or try with <script>jfun("<?php echo $i;?>");</script> 或尝试使用<script>jfun("<?php echo $i;?>");</script>

Write the code before the PHP code. 在PHP代码之前编写代码。 In that case the function jfun is known. 在这种情况下,函数jfun是已知的。

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

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