jquery基于user_id的动态href

发布于 2024-12-08 08:14:28 字数 320 浏览 3 评论 0原文

我已经输入了这个小 Jquery 来根据用户 ID 动态附加我的计费页面的 href。

它似乎不起作用?希望有人能指出我的错误。

预先感谢您的帮助和代码片段。

$individualpage="billing".$_SESSION['user_id'].".php";
$("a#billing").attr("href", "$individualpage");

页面上有链接

<a href="" id="billing">billing</a>

I have typed up this little Jquery to dynamically append the href of my billing page based on the users id.

It doesn't seem to be working tho? Hopefully someone can point out my mistake.

Thanks in advance for your help and code snippets.

$individualpage="billing".$_SESSION['user_id'].".php";
$("a#billing").attr("href", "$individualpage");

Link on page

<a href="" id="billing">billing</a>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄凉少年不暖心 2024-12-15 08:14:28

您将 JavaScript(一种客户端语言)与 PHP(一种服务器端语言)混合在一起。请记住,PHP 是在 JavaScript 开始时就完成的。

,请尝试如下操作:

PHP 页面中的某处:

<?php
  $individualpage="billing".$_SESSION['user_id'].".php";
?>

然后,在页面的 HTML 中,使用 PHP 将值转储到 JavaScript,以便它可以拾取并完成请求:

$('a#billing').attr('href','<?= $individualpage; ?>');

话虽如此 ,您最好直接将其转储到 标记中:(

<a id="billing" href="<?= $individualpage; ?>" ... >...</a>

并且完全跳过 JavaScript。)

You're mixing JavaScript (a client-side language) with PHP (a server-side language). Remember that PHP is done by the time JavaScript begins.

With that said, try something like this:

Somewhere in your PHP page:

<?php
  $individualpage="billing".$_SESSION['user_id'].".php";
?>

Then, within the HTML of the page, use PHP to dump the value to JavaScript so it can pick up and complete the request:

$('a#billing').attr('href','<?= $individualpage; ?>');

Although, you're probably better off just dumping it directly in to the <a> tag:

<a id="billing" href="<?= $individualpage; ?>" ... >...</a>

(And skipping JavaScript altogether.)

浮生未歇 2024-12-15 08:14:28

您可能将 PHP 和 JS 代码混合在一起。

您需要

<script>
$(function() {
    var individualpage="billing<?php echo $_SESSION['user_id'] ?>.php";
    $("a#billing").attr("href", individualpage);
});
</script>

在视图脚本中。

Your probably mixing PHP and JS code together.

You need

<script>
$(function() {
    var individualpage="billing<?php echo $_SESSION['user_id'] ?>.php";
    $("a#billing").attr("href", individualpage);
});
</script>

in your view script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文