With JQuery I can easily do an AJAX post of key value pairs without a form and without a page refresh:
使用JQuery,我可以轻松地在没有表单且没有页面刷新的情况下执行关键值对的AJAX帖子:
$.ajax({ type: 'POST', url: url, data: { key: value, key: value, etc...} });
But is there a way to post a set of non-form data values with a page refresh, or do I need to take the conventional route of setting up a set of form elements, loading their values, and then submitting the form?
但是有没有办法通过页面刷新发布一组非表单数据值,或者我是否需要采用传统的方法来设置一组表单元素,加载它们的值,然后提交表单?
thanks!
4 个解决方案
#1
5
You could go the conventional route, but have jQuery build and populate a form dynamically if you wanted to avoid putting a <form>
on the page.
您可以使用传统路径,但如果您想避免在页面上放置
#2
1
You could refresh the page after the ajax post:
您可以在ajax帖子后刷新页面:
$.ajax({
type: 'POST',
url: url,
data: {
key1: value1,
key2: value2,
etc...
},
complete: function () {
window.location.reload(true);
}
});
But what's wrong with doing it the old-fashioned way?
但是用老式的方式做错了什么呢?
window.location.reload @ MDC
#3
1
You can create dynamic form via jQuery, append it to body and submit it. Disadvantage: you have to generate all fields for it.
您可以通过jQuery创建动态表单,将其附加到正文并提交。缺点:您必须为其生成所有字段。
$('#Setting_type').change(function () {
$('<form/>', {
action: "/postReceiver.php",
method: "post",
html: "<input name='Setting[key]' value='"+$('#Setting_key').val()+"'>"
+"<input name='Setting[type]' value='"+$('#Setting_type').val()+"'>"
+"<input name='typeChange' value='1'>",
class: "hidden"
})
.appendTo('body')
.submit()
;
});
If you just want to send data via POST but still show params in query, just omit html
attribute and put everything in action
如果您只想通过POST发送数据但仍然在查询中显示params,只需省略html属性并将所有内容付诸实践
$('#Setting_type').change(function () {
$('<form/>', {
action: "/postReceiver.php?type="+encodeURIComponent($('#Setting_name').val())
+"&Setting[key]="+encodeURIComponent($('#Setting_key').val()),
method: "post",
class: "hidden"
})
.appendTo('body')
.submit()
;
});
.hidden {
display: none;
}
#4
0
you can always do
你可以随时做
window.location = //wherever you want go
On ajax success
关于ajax的成功
#1
5
You could go the conventional route, but have jQuery build and populate a form dynamically if you wanted to avoid putting a <form>
on the page.
您可以使用传统路径,但如果您想避免在页面上放置
#2
1
You could refresh the page after the ajax post:
您可以在ajax帖子后刷新页面:
$.ajax({
type: 'POST',
url: url,
data: {
key1: value1,
key2: value2,
etc...
},
complete: function () {
window.location.reload(true);
}
});
But what's wrong with doing it the old-fashioned way?
但是用老式的方式做错了什么呢?
window.location.reload @ MDC
#3
1
You can create dynamic form via jQuery, append it to body and submit it. Disadvantage: you have to generate all fields for it.
您可以通过jQuery创建动态表单,将其附加到正文并提交。缺点:您必须为其生成所有字段。
$('#Setting_type').change(function () {
$('<form/>', {
action: "/postReceiver.php",
method: "post",
html: "<input name='Setting[key]' value='"+$('#Setting_key').val()+"'>"
+"<input name='Setting[type]' value='"+$('#Setting_type').val()+"'>"
+"<input name='typeChange' value='1'>",
class: "hidden"
})
.appendTo('body')
.submit()
;
});
If you just want to send data via POST but still show params in query, just omit html
attribute and put everything in action
如果您只想通过POST发送数据但仍然在查询中显示params,只需省略html属性并将所有内容付诸实践
$('#Setting_type').change(function () {
$('<form/>', {
action: "/postReceiver.php?type="+encodeURIComponent($('#Setting_name').val())
+"&Setting[key]="+encodeURIComponent($('#Setting_key').val()),
method: "post",
class: "hidden"
})
.appendTo('body')
.submit()
;
});
.hidden {
display: none;
}
#4
0
you can always do
你可以随时做
window.location = //wherever you want go
On ajax success
关于ajax的成功