【问题标题】:Passing URL (GET) var to Javascript将 URL (GET) var 传递给 Javascript
【发布时间】:2023-03-09 19:49:01
【问题描述】:

我正在使用 wordpress,将内容从自定义页面加载到我的主题中。我所拥有的是:

header.php

<script type="text/javascript">
$("#another").click(function(){
   $("#randomPost")
            .text("... loading ...")
            .load("/wp/ajax-page/?id=<? echo $id; ?>+cachebuster=" + Math.floor(Math.random()*10001));
   return false;
});
</script>

index.php

<a href="#?id=1" id="another">Get another!</a>

ajax_page.php (http://localhost/wp/ajax-page/)

<?php
/*
Template Name: AJAX
*/
?>

<?
$id = $_GET['id'];
?>
<?php 
    query_posts('showposts=1&id='.$id.''); 
    the_post();
?>

<a href='<?php the_permalink(); ?>'><?php the_title(); ?></a>

一切正常,这是我发现将数据加载到 wordpress 下的 div 中的更简单方法(使用 javascript)。我的问题是我无法通过 URL 传递 id Var,有什么想法吗?

非常感谢各位,希望你们能帮帮我。

【问题讨论】:

    标签: php javascript wordpress url


    【解决方案1】:
    <script type="text/javascript">
    $("#another").click(function(){
       $("#randomPost")
                .text("... loading ...")
                .load("/wp/ajax-page/?id=<?php echo $id; ?>&cachebuster=" + Math.floor(Math.random()*10001));
       return false;
    });
    </script>
    

    问题出在……负载上。您正在打印 $id 但您忘记添加 & 所以您发送 id 参数和 cachebuster 参数。他们在一起。

    【讨论】:

    • 这很好用!
    • 我忘了。请参阅这篇文章link。使用函数获取 GET 参数,而不是将其打印在 javascript 代码上。有很多类似的功能,但这是google上的第一个链接=P
    【解决方案2】:

    尝试将&lt;? echo $id; ?&gt;改为document.URL.split('#?id=')[1]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多