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.
推荐答案
稍微整理了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 定义,但在 body 标记的末尾不会显示任何表单.用法也稍微简单一些,例如:
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屋!