【问题标题】:How do I insert a $variable into jQuery如何将 $ 变量插入 jQuery
【发布时间】:2009-12-29 06:02:35
【问题描述】:

我正在使用 jQuery 在 Drupal 中工作。如何在标签中插入 php $variable。

$(document).ready(function(){
    $("#comment-delete-<?php print $variable ?>").click(function(){
        $("div.comment-<?php print $variable ?> span.body").replaceWith("new text");
    });
})

$(document).ready(function(){
    $("#comment-delete-". $variable).click(function(){
        $("div.comment-". $variable ." span.body").replaceWith("new text");
    });
})

有几点需要澄清。 我在 Drupal 中运行,所以完整的代码如下所示:

<?php
drupal_add_js (
    '$(document).ready(function(){
        $("#comment-delete-"' print $comment->cid; ').click(function(){
            $("div.comment-"' print $comment->cid; '" span.body").replaceWith("<span style=\'color: grey;\'>Please wait...</span>");
        });
    })',
'inline');
?>

但它仍然无法正常工作。

更新:我尝试了以下方法,但仍然无法正常工作

<?php
$testing = "42";
drupal_add_js (
    '$(document).ready(function(){
        $("#comment-delete-"'. $testing .').click(function(){
            $("div.comment-"'. $testing .'" span.body").replaceWith("<span style=\'color: grey;\'>Please wait...</span>");
        });
    })',
'inline');
?>

如果我使用数字“42”而不是变量,它可以工作,但在使用变量时却不行……很奇怪。

【问题讨论】:

  • 你试过了吗?第一种方法应该适合你
  • 我在 Drupal,我正在使用 print $variable print $variable 但它不起作用
  • 没关系...这行不通...但是正如您所见,我已经在 php 中了
  • 我无法退出 插入一个 php 变量
  • 你有几个错位的引号..

标签: php jquery drupal


【解决方案1】:
$(document).ready(function(){
    $("#comment-delete-<?php print $variable ?>").click(function(){
        $("div.comment-<?php print $variable ?> span.body").replaceWith("new text");
    });
})

因为 PHP 是在页面加载之前执行的,所以第二种方法不起作用。实际上,第二个混合了两种在不同时间运行的不同语言,这意味着......它仍然无法工作。


这就是发生的事情。

浏览器请求页面

PHP 创建 HTML 页面
PHP 在文件中搜索 &lt;?php ?&gt; 标记,并运行其中的代码:

$(document).ready(function(){
    $("#comment-delete-<?php print $variable ?>").click(function(){
        $("div.comment-<?php print $variable ?> span.body").replaceWith("new text");
    });
})

上面的例子会在解析后创建这个:

$(document).ready(function(){
    $("#comment-delete-mytag").click(function(){
        $("div.comment-mytag span.body").replaceWith("new text");
    });
})

服务器向浏览器发送页面

浏览器读取页面

Javascript 运行:

$(document).ready(function(){
    $("#comment-delete-mytag").click(function(){
        $("div.comment-mytag span.body").replaceWith("new text");
    });
})

如果您注意到,PHP 只是创建了要发送到浏览器的网页。因此,您使用 PHP 所做的就是创建 Javascript 代码。在 PHP 中工作时,您实际上不必遵循任何 Javascript 语法规则。当它到达浏览器时,您必须简单地使 Javascript 语法正确。 AKA 你可以插入所有你想要的&lt;?php ?&gt; 标签,只要页面到达浏览器时,它是有效的Javascript。


如果您将代码传递给函数,就像您的评论所述,您将创建一个字符串,该字符串封装在引号中,在这种情况下,您的第二个示例将是正确的:

drupal_add_js (
    '$(document).ready(function(){
        $("#comment-delete-'. $variable . ').click(function(){
            $("div.comment-'. $variable . ' span.body").replaceWith("<span   style=\'color: grey;\'>Please wait...</span>");
        });
    })',
'inline');

【讨论】:

    【解决方案2】:

    根据您的评论:

    <?php 
        drupal_add_js ('
            $(document).ready (function() { 
                $("#comment-delete-' . $variable . '").click (function() { 
                    $("div.comment-' . $variable . ' span.body").replaceWith ("new text"); 
                }); 
            })
        ','inline');
    ?>
    

    你应该连接$variable,而不是print-ing它

    【讨论】:

      【解决方案3】:

      [edit] 哎呀,没关系,我刚刚意识到确切的答案已经发布了,我只是不够小心,没有关注它..

      【讨论】:

      • 请单击我们的答案之一旁边的复选框以将其标记为正确。谢谢!很高兴您的问题得到了解决。
      猜你喜欢
      • 2016-08-01
      • 2015-12-21
      • 2010-12-12
      • 2014-10-16
      • 2013-02-20
      • 1970-01-01
      • 2012-05-08
      • 1970-01-01
      • 2015-02-16
      相关资源
      最近更新 更多