如何在jQuery中插入一个$变量 [英] How do I insert a $variable into jQuery

查看:390
本文介绍了如何在jQuery中插入一个$变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery在Drupal工作。如何将php $变量插入到标签中。

  $(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:gray; \'>等待...< / span>);
});
})',
'inline');
?>

但仍然无法使用。



更新:我尝试了以下,但仍然不起作用

 <?php 
$ testing =42 ;
drupal_add_js(
'$(document).ready(function(){
$(#comment-delete-'$ test。 b $ b $(div.comment-'。$ testing。'span.body)。replaceWith(< span style = \'color:gray; \'>请稍候...< ; / span>);
});
})',
'inline');
?>

如果我使用数字42而不是变量,它可以工作,但不使用

解决方案

根据您的评论:

 <?php 
drupal_add_js('
$(document).ready(function(){
$(#comment-delete- $ variable $$$$$$$$$$ ;
})
','inline');
?>

您应该连接 $变量 print -ing it


I'm working in Drupal with jQuery. How do I insert a php $variable into a tag.

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

or

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

A few things to clearify. I'm running in Drupal, so the full code looks like this:

<?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');
?>

but it's still not working.

Update: I tried the following, but it's still not working

<?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');
?>

If I use the number "42" instead of the variable, it works, but not when using the variable... weird.

解决方案

Based on your comment:

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

You should concatenate the $variable, instead of print-ing it

这篇关于如何在jQuery中插入一个$变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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