从php if语句运行JavaScript函数 [英] Run a JavaScript function from a php if statement

查看:454
本文介绍了从php if语句运行JavaScript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP条件,想知道我是否可以运行JavaScript函数,而不必使用JavaScript点击它:

I am using PHP conditions and want to know if I can run a JavaScript function without some one have to click on it using JavaScript:

if($value == 1)     
{  
    JavaScript to run the function  
}

我该怎么做?

推荐答案

首先请记住你的PHP代码在服务器上进行评估,而JavaScript则在客户端的浏览器中运行。这些评估发生在不同的地方,在不同的时间。因此,您无法从PHP调用JavaScript函数。

First of all keep in mind that your PHP code is evaluated on the server, while JavaScript runs in the browser on the client-side. These evaluations happen in different places, at different times. Therefore you cannot call a JavaScript function from PHP.

但是使用PHP,您可以呈现HTML和JavaScript代码,使其仅在PHP条件为真时呈现。也许你可能想尝试这样的事情:

However with PHP you can render HTML and JavaScript code such that it is only rendered when your PHP condition is true. Maybe you may want to try something like this:

if($value == 1) {
   echo "<script>";
   echo "alert('This is an alert from JavaScript!');";
   echo "</script>";
} 

这篇关于从php if语句运行JavaScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆