javascript - 如何在没有表单的情况下使用 Jquery 提交链接点击页面?

标签 javascript jquery post submit client-side

我想在点击链接时伪造页面提交(发布)。我需要在 post 中传递两个参数,即 id (下面代码中的 var“x”,以及 Submitted=“true”)到页面 index.php。我需要伪造一个提交(全页加载),而不仅仅是一个 ajax 请求。

<a href="#" id="5" class="rootcontent">Title</a>

$("a.rootcontent").live('click', function(){
    var x= $(this).attr("id"); 
   //need the code to submit the page to index.php with two $_POST variables (x and submitted=true)
    });

最佳答案

一个快速的解决方案是将表单附加到文档中并提交。类似于以下内容(未经测试,但应该有效):

var form = $('<form method="post" id="form-to-be-submitted" action="/index.php"><input type="hidden" name="x" value="' + x + '"/><input type="hidden" name="submitted" value="true"/></form>');
$(document.body).append(form);
form.submit();

关于javascript - 如何在没有表单的情况下使用 Jquery 提交链接点击页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8050840/

相关文章:

javascript - 如何扩展 Javascript 中的内置对象,例如 Math?

javascript - 让网页看起来完全加载,或者 jQuery 到 MooTools 的翻译

asp.net-mvc - 为什么 ASP.NET MVC 默认 Model Binder 很慢?它需要很长时间才能完成工作

javascript - 从 jquery 调用 spring 操作,无需使用 ajax 来实现多个按钮(例如 View 编辑等)

javascript - 尝试使用 jQuery.ajax 发送 FormData 时出错

javascript - 将异步函数传递给 promise.all()

javascript 变量未设置 - 通过 Firebug 检查

jquery - 以编程方式禁用 nvd3 水平多条形图上的系列

Python POST 请求返回 401

post - client1.google.com/ocsp 是什么?