How do I insert a $variable into jQuery

2019-08-16 19:02发布

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.

3条回答
爷、活的狠高调
2楼-- · 2019-08-16 19:15

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

查看更多
ら.Afraid
3楼-- · 2019-08-16 19:23

[edit] Ooops, never mind, i just realized that the exact answer has been already posted, i just wasn't careful enough to follow it..

查看更多
虎瘦雄心在
4楼-- · 2019-08-16 19:28
$(document).ready(function(){
    $("#comment-delete-<?php print $variable ?>").click(function(){
        $("div.comment-<?php print $variable ?> span.body").replaceWith("new text");
    });
})

Because PHP is executed BEFORE the page loads, the second method would not work. Actually, the Second one mixes two different languages that run at different times, which means ... it still won't work.


This is what happens.

Browser Requests Page

PHP Creates the HTML Page
PHP searches the file for <?php ?> tags, and runs the code inside them:

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

The above example would create this after being parsed:

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

Server Sends Page to Browser

Browser reads the page

Javascript Runs:

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

If you notice, PHP simply creates the webpage to be sent to the browser. So all you use PHP to do is to create the Javascript Code. When working in PHP, you never actually have to follow any Javascript Syntax Rules. You must simply make the Javascript Syntax correct when it gets to the browser. AKA You can insert all the <?php ?> tags you want, as long as when the page gets to the browser, it is valid Javascript.


If you are passing the code into a function, like your comment stated, you would be creating a string, which is encapsulated in quotes, in which case your second example would be correct:

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');
查看更多
登录 后发表回答