我试图使用jQuery使用下面的代码
file.js
$('#sidebar-nav').on('click', 'li', function() {
var theID = $(this).attr('id');
$.post(location.href, {theID: theID});
});
file.php
<?php
if (isset($_POST['theID'])) {
// do something
}
?>
<html>
<!-- the rest of the page -->
</html>
张贴到同一页的变量
已更新
但是由于某种原因,theID
没有发布到php文件中。我的代码在if
语句中将不会执行。另外,我在同样的js和相同的php文件中对其他removeID
使用了完全相同的方法,它对removeID
运行良好,但不是theID
。
并澄清什么location.href
是在这种情况下;它是file.php
。
任何人都可以帮助我吗?
什么不行? – David
应该发生什么?使用'$ .post()'就可以向PHP发出请求,但浏览器中的页面不会有明显的变化。通过“这不起作用”你的意思是你的PHP没有收到请求,或...? – nnnnnn
location.href是什么?我们不能猜测你的变量。他们是可变的。 – Jimbo