首页 > 解决方案 > 如何使用js获取post ID php

问题描述

这里我想使用js根据点击按钮显示数据id

我的代码:

var idPost = document.getElementsByClassName("getID")[0];
idPost.onclick = function() {
    var GetID = window.location.protocol + "//" 
                + window.location.host 
                + window.location.pathname 
                + '?id=<?php echo $id;?>';
    window.history.pushState({ path: GetID }, '', GetID );
}

为什么我的代码不起作用?也许这里有人可以解决我的问题

标签: javascriptphp

解决方案


这可能会帮助您:

编辑

location.href重新加载页面以指示url

var idPost = document.getElementsByClassName("getID")[0];
idPost.onclick = function() {
    let id = "<?php echo $id; ?>";
    location.href = `?id=${id}`;
}

推荐阅读