I am trying to use the jQuery POST function but it is handling the request in AJAX style. I mean it's not actually going to the page I am telling it to go.

$("#see_comments").click(function() {
    $.post(
        "comments.php", 
        {aid: imgnum}, 
        function (data) {

        }
    );
});

此函数应转到comments.php页,且手头有援助值.它发布得很好,但不能重定向到comments.php.


@Doug Neiner澄清:

  1. 我有15个链接(图片).我点击一个链接,它就会加载我的JavaScript.脚本知道我打开了什么.这是我想要的comments.php中的imgnum.我必须使用这个JavaScript,没有其他方法可以做到这一点.JavaScript是必需的

  2. 您的方法成功地发布了aid值.但在comments.php中,当我try echo 该值时,它什么也没有显示.

  3. I am using Firebug. In the Console, it shows the echo REQUEST I made in Step (2) successfully.

推荐答案

我知道你想做什么,但那不是你想要的.

首先,除非服务器上有changing个数据,否则不要使用POST请求.只要让#see_comments做一个正常的<a href='/comments.php?aid=1'>...就好了

If you have to use POST, then do this to get the page to follow your call:

$("#see_comments").click(function() {
  $('<form action="comments.php" method="POST">' + 
    '<input type="hidden" name="aid" value="' + imgnum + '">' +
    '</form>').submit();
});

How this would actually work.

First $.postonly个Ajax方法,不能像您所描述的那样用于执行传统的form提交.因此,为了能够发布值and导航到新页面,我们需要模拟form发布.

So the flow is as follows:

  1. You click on the image, and your JS code gets the imgnum
  2. Next, someone clicks on #see_comments
  3. We create a temporary form with the imgnum value in it as a hidden field
  4. We submit that form, which posts the value and loads the comments.php page
  5. 您的comments.php页将可以访问发布的变量(即在PHP中为$_POST['aid'])

Jquery相关问答推荐

为什么如果使用转换规模,juserui可拖动遏制不起作用

Django BaseDataTableView-Filter_queryset方法不起作用

如果文本框内容在 X 秒内没有更改,则进行 post 调用

如何在for循环期间上传文件

如何在 jquery 中切换 attr()

使用 jQuery / javascript 测试链接是否是外部的?

如何使用 jQuery 获取所有 ID?

我如何从 ACE 编辑器中获得价值?

未捕获的类型错误:$.post 不是函数

如何在 HTML选项标签上显示工具提示?

jQuery中追加的相反

在 HTML 表单提交上制作 Enter 键而不是激活按钮

如何使用 jQuery 进行带命名空间的 XML 解析

将 CSS 应用于 jQuery 对话框按钮

如何通过 DOM 容器访问 Highcharts 图表?

使用 jquery 在 radio 上单击或更改事件

如何在javascript中获取json键和值?

如何在 JavaScript / jQuery 中获取对象的属性?

Jquery live() 与委托()

如何在 jQuery 中取消绑定悬停?