【问题标题】:Pass javascript variable by jquery通过 jquery 传递 javascript 变量
【发布时间】: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


    【解决方案1】:

    usermessage.php 包含一个 HTML &lt;script&gt; 对象,但 index.php 永远不会对从 AJAX 调用 (data) 接收到的 HTML 做任何事情。您必须将收到的 HTML 附加到主 DOM。

    相反,我建议 usermessage.php 以纯文本(或 JSON)形式返回 lastMsgID,然后将其保存在变量 data 中。

    更新

    您已经声明了从封闭范围访问检索到的值的要求。通过创建一个“更全局”的变量并相应地设置它的值来做到这一点:

    <script type="text/javascript">
        var MyVariable = ''; // global variable: changes to it will be global too
    
        function myfunction() {
            jQuery.get("usermessage.php, function(data) {
               MyVariable = data;
            });
        };
    
        document.write(MyVariable);
        /* ^ this will work, but not quite here. it'll show you the value
           ONLY if you write this line somewhere so that it executes *after*
           the jQuery.get call has completed. */
    </script>
    

    【讨论】:

    • 我无法使用数据来检索变量,因为该数据已用于检索列表其他消息。有没有其他方法可以在不使用数据的情况下获取变量值?
    • @zac1987:不,data 是你应该这样做的。如果data 已经携带信息,那么只需将lastMsgID 添加到data 携带的事物列表中。如果data 是 JSON 数组/对象,那么这很容易。
    • 好的,我按照你说的做。我使用数据来检索变量。这是我的代码: document.write(data); myfunction(){ jQuery.get("usermessage.php", function(data) { document.write(data); });但它仍然是一样的,我只能在函数内部显示变量“数据”的值,我不能在函数外部显示它。如何在函数外显示? – zac1987 1 小时前
    • @zac1987:“函数之外”对您意味着什么?如果您之前已将变量声明为存在于封装范围中,则可以使用赋值运算符 (=) 为该变量赋予来自 data 的新值。
    • “函数外”是指关闭函数后的 i document.write(variable)。我已经发布了新答案以向您展示示例,请参阅新答案。谢谢。
    【解决方案2】:

    PHP

    <?php
    $latestmsgid = Array("id" => "lovelovelove");
    echo json_encode($lastmsgid);
    ?>
    

    JS

    var ID;
    
    $.getJSON("usermessage.php", function(json) {
        ID = json.id;
    });
    

    【讨论】:

      猜你喜欢
      • 2011-10-19
      • 2018-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-23
      相关资源
      最近更新 更多