【发布时间】:2013-12-09 00:51:28
【问题描述】:
我正在尝试使用 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。
有人可以帮我解决这个问题吗?
【问题讨论】:
-
应该发生什么?像这样使用
$.post()应该向您的PHP 发出请求,但是浏览器中的页面不会有可见的变化。 “这不起作用”是指您的 PHP 没有收到请求,还是...? -
location.href 是什么?我们无法猜测您的变量。它们是可变的。
-
抱歉声明不清楚,我更新了问题。我的 php 文件没有收到请求。
-
还有
{"theID": theID} -
Jimbo的意思是
location.href在你使用的时候的当前值是多少...
标签: javascript php jquery post