jQuery非AJAX POST [英] jQuery non-AJAX POST
问题描述
jQuery中是否有一个简单的非AJAX POST方法?
Is there a simple non-AJAX POST method in jQuery?
我正在寻找与页面上的表单等效的东西,只有通过JavaScript设置的隐藏字段然后获得POST后,导致浏览器通过 action
加载页面集。只是一个普通的POST,但是通过jQuery设置了值。
I am looking for something equivalent to a form on a page with nothing but hidden fields set via JavaScript which then gets POST-ed, causing the browser to load the page set via action
. Just a normal POST, but with values set via jQuery.
我想我可以继续实现我当前的方法,但我很好奇jQuery是否提供了一种快速方法。在后台,我想它会用所有隐藏的值动态创建这个表单并提交它。
I suppose I could keep implementing my current method, but I am curious if jQuery provides a quick way. In the background, I imagine it would dynamically create this form with all of the hidden values and submit it.
推荐答案
Tidied Darin的优秀稍微解决。
Tidied Darin's excellent solution slightly.
function myFunction(action, method, input) {
'use strict';
var form;
form = $('<form />', {
action: action,
method: method,
style: 'display: none;'
});
if (typeof input !== 'undefined' && input !== null) {
$.each(input, function (name, value) {
$('<input />', {
type: 'hidden',
name: name,
value: value
}).appendTo(form);
});
}
form.appendTo('body').submit();
}
这是兼容JSLint的,并确保最后不显示任何表格尽管可能的CSS定义,身体标签。用法也稍微简单明了,例如:
This is JSLint-compatible and makes sure that no form is displayed at the end of body tag despite possible css definitions. The usage is also slightly more straightforward, e.g:
myFunction('/path/to/my/site/', 'post', {
id: 1,
quote: 'Quidquid Latine dictum sit altum videtur'
});
这篇关于jQuery非AJAX POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!