如何发送POST数据并使用JQuery导航? [英] How can I send POST data and navigate with JQuery?
问题描述
在我的博客上,我有很多< pre>
包含代码段的块。
On my blog I have a lot of <pre>
blocks containing code snippets.
我是什么想要做的是在页面上的所有< pre>
元素中添加 .click()
处理程序将其内容发送到另一个页面 - 让我们称之为 viewcode.php
- 通过POST。
What I want to do is add a .click()
handler to all the <pre>
elements on the page which will send its content to another page - let's call it viewcode.php
- via POST.
我知道如何发送使用 $。ajax
访问此页面的信息,我只是不确定如何发送信息和导航到该页面。
I know how to send information to this page using $.ajax
, I'm just not sure how to send the information and navigate to the page.
想法是访问者可以点击< pre>
,这将导航到另一个包含代码的页面,以便于阅读并且很容易复制/粘贴。
The idea is that visitors can click a <pre>
which will navigate to another page containing the code on its own for readability and easy copy / paste.
我觉得解决方案很简单,可能很明显,我想不出来。
I have a feeling the solution is dead simple and probably obvious, I just can't think of it.
推荐答案
不确定我会这样处理它,可能我只是弹出一个包含代码的对话框而不是离开页面,但你可以通过建造一个表单使用javascript然后触发该表单上的提交而不是使用AJAX。
Not sure I would handle it this way, probably I would simply pop up a dialog with the code rather than leave the page, but you could handle this by building a form using javascript then triggering a submit on that form instead of using AJAX.
使用带有jQuery UI的对话框:
Using dialogs with jQuery UI:
$('pre').on('click', function() {
$('<div title="Code Preview"><p>' + $(this).text() + '</p></div>').dialog({
... set up dialog parameters ...
});
});
建立表格
$('pre').on('click', function() {
var text = $(this).text();
$('<form class="hidden-form" action="something.php" method="post" style="display: none;"><textarea name="code"></textarea></form>')
.appendTo('body');
$('[name="code"]').val(text);
$('.hidden-form').submit();
});
这篇关于如何发送POST数据并使用JQuery导航?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!