Javascript 如何使用js获取post-ID-php
在这里,我想使用js显示基于单击按钮的数据id 我的代码:Javascript 如何使用js获取post-ID-php,javascript,php,Javascript,Php,在这里,我想使用js显示基于单击按钮的数据id 我的代码: var idPost = document.getElementsByClassName("getID")[0]; idPost.onclick = function() { var GetID = window.location.protocol + "//" + window.location.host + window.location.pathname
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 );
}
var idPost=document.getElementsByClassName(“getID”)[0];
idPost.onclick=函数(){
var GetID=window.location.protocol+“/”
+window.location.host
+window.location.pathname
+“?id=”;
pushState({path:GetID}',,GetID);
}
为什么我的代码不起作用?
也许这里有人可以帮我解决问题这可能会帮助你: 编辑
location.href
将页面重新加载到指定的url
var idPost = document.getElementsByClassName("getID")[0];
idPost.onclick = function() {
let id = "<?php echo $id; ?>";
location.href = `?id=${id}`;
}
var idPost=document.getElementsByClassName(“getID”)[0];
idPost.onclick=函数(){
让id=“”;
location.href=`?id=${id}`;
}
这可能会帮助您:
编辑
location.href
将页面重新加载到指定的url
var idPost = document.getElementsByClassName("getID")[0];
idPost.onclick = function() {
let id = "<?php echo $id; ?>";
location.href = `?id=${id}`;
}
var idPost=document.getElementsByClassName(“getID”)[0];
idPost.onclick=函数(){
让id=“”;
location.href=`?id=${id}`;
}
如果你有da技能,AJAX会在treyBake历史上支付da账单。pushState
将一个新的状态推到历史上-就是这样。它不会加载任何内容——您自己在后台加载,并将其放入当前文档中,这是您的责任。或者,您可以只将新URL分配给位置.href
-然后这将是一个真正的导航操作,自动导致服务器请求加载新文档以显示。顺便说一句,前缀为window.location.protocol+“/”+window.location.host
有点多余-存在相对URL。你可以使用jquery并通过ajax调用发送id。如果你掌握了da技能,ajax会在treyBakehistory支付da账单。pushState
将一个新状态推到历史记录中-就是这样。它不会加载任何内容——您自己在后台加载,并将其放入当前文档中,这是您的责任。或者,您可以只将新URL分配给位置.href
-然后这将是一个真正的导航操作,自动导致服务器请求加载新文档以显示。顺便说一句,前缀为window.location.protocol+“/”+window.location.host
有点多余-存在相对URL。您可以使用jquery并通过ajax调用发送id。