简体   繁体   English

在Ajax请求之后调用JavaScript函数

[英]Call a JavaScript function after an Ajax request

I have a problem with executing a JavaScript function after an Ajax request with JQuery. 我在通过JQuery请求Ajax之后执行JavaScript函数时遇到问题。

First, I call a PHP page with an Ajax request, everything works fine. 首先,我用Ajax请求调用PHP页面,一切正常。

In this PHP page, I return a JavaScript call for a function. 在此PHP页面中,我返回了对函数的JavaScript调用。

But when I append this script on my HTML page, Firebug return this : "ReferenceError: getNumber is not defined" getNumber(55); 但是,当我将此脚本附加到HTML页面上时,Firebug会返回以下内容: "ReferenceError: getNumber is not defined" getNumber(55); . (55 is just an example, it can be from 0 to 100) (55只是一个示例,它可以是0到100)

HTML (Jquery and myScript are loaded correctly): HTML (正确加载了Jquery和myScript):

<script src="jquery1111min.js"></script>
<script src="myScript.js"></script>
<div id="content_modal" class="content_modal">

</div>

JQuery (myScript.js): jQuery (myScript.js):

$.get("inc/get_something.php", function(data) {
  $(".content_modal").html(data);//Works
});
function getNumber(nb) {
  alert(nb);//never called
}

PHP (get_something.php): PHP (get_something.php):

//some html
$number = mt_rand(0,100);
<script>getNumber(<?=$number?>);</script>

Cheers 干杯

Is your 是你的

function getNumber(nb) { ... } 

oustide your 驱逐你的

$(document).ready()

or 要么

$(window).load()

If not, try putting it outside anything, in top of your tag 如果没有,请尝试将其放在标签顶部之外的任何位置

You cannot execute javascript from a php page that isn't rendered in the browser. 您无法从未在浏览器中呈现的php页面执行javascript。 JavaScript is rendered in the browser and not on the server. JavaScript在浏览器中呈现,而不在服务器上呈现。

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

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