【发布时间】:2011-07-23 21:50:37
【问题描述】:
index.php
<script type="text/javascript">
document.write(lastmsgID); // fail display the value (my problem is here, please help)
function myfunction()
{
jQuery.get("usermessage.php, function(data) {
document.write(lastmsgID); //success display the value
});
};
</script>
用户信息.php
<?php $latestmsgid= "lovelovelove";
echo("<script type='text/javascript'>\n"); // pass php varible to javascript global variable lastmsgID
echo("lastmsgID = '". $latestmsgid ."';\n");
echo("</script>");
?>
根据上面的代码,我在 usermessage.php 文件中声明了全局 javascript 变量 lastmsgID。然后我使用 jQuery.get 获取变量的值。现在我的问题是我只能在 myfunction() 函数内部获取变量值,如何将变量的值从函数传递到函数外部?
更新:
<script type="text/javascript">
function myfunction()
{
jQuery.get("usermessage.php, function(data) {
var test=data
document.write(test); //success display the value
});
};
document.write(test); // fail display the value because it is outside the function. (my problem is here, please help)
</script>
【问题讨论】:
标签: javascript jquery parameter-passing