用javascript发送post变量?
可以用javascript发送post变量吗? 我希望用javascript发送post变量?,javascript,html,Javascript,Html,可以用javascript发送post变量吗? 我希望id与post一起发送,而不是get window.location.href="hanteraTaBortAnvandare.php?id=10"; 您可以通过JavaScript提交表单数据,但不能使用window.location.href。更改URL位置总是会发出GET 您需要从DOM中获取对表单的引用,然后对其发出submit()调用。在这种情况下,您可以通过Ajax请求(或使用隐藏表单)完成此操作 例如: new Request
id
与post一起发送,而不是get
window.location.href="hanteraTaBortAnvandare.php?id=10";
您可以通过JavaScript提交表单数据,但不能使用window.location.href。更改URL位置总是会发出GET
您需要从DOM中获取对表单的引用,然后对其发出submit()调用。在这种情况下,您可以通过Ajax请求(或使用隐藏表单)完成此操作 例如:
new Request({
url: 'hanteraTaBortAnvandare.php',
method: 'post',
data: {
'id': '10'
},
onComplete: function(response) {
alert(response);
}
});
$.ajax({
type: 'post',
url: 'hanteraTaBortAnvandare.php',
data: 'id=10',
success: function(response) {
alert(response);
}
});
例如:
new Request({
url: 'hanteraTaBortAnvandare.php',
method: 'post',
data: {
'id': '10'
},
onComplete: function(response) {
alert(response);
}
});
$.ajax({
type: 'post',
url: 'hanteraTaBortAnvandare.php',
data: 'id=10',
success: function(response) {
alert(response);
}
});
当然,您可以在没有外部库的情况下实现这一点,但它们简化了很多 最简单的方法就是在页面中添加一个表单:
<form method="POST" action="hanteraTaBortAnvandare.php" id="DeleteUserForm">
<input type="hidden" name="id" value="10" />
</form>
您可以使用表单,然后使用
document.getElementById('id_of the_form').submit()代码>
表单不需要编写为纯HTML:您可以通过以下方式创建它:
function postIt() {
form = document.createElement('form');
form.setAttribute('method', 'POST');
form.setAttribute('action', 'someURL');
myvar = document.createElement('input');
myvar.setAttribute('name', 'somename');
myvar.setAttribute('type', 'hidden');
myvar.setAttribute('value', 'somevalue');
form.appendChild(myvar);
document.body.appendChild(form);
form.submit();
}
代码示例可以使用+1。您可能会考虑包括一个链接到MOOToOK。是的,但是在发送表单之前,我希望“代码>警报())/代码>用户符合(是或否)发送表单。这很简单,只需在IF语句中使用确认:IF(Window)确认(“VIL DuvikLigEN RADEA NavDAREN?”){表单…}这应该是答案。如果答案解决了你的问题,请接受。请不要提供“W3傻瓜”(请用谷歌搜索)参考资料,他们的封闭(非维基类型)表格不能提供高质量的知识。替代链接:@Akshaykn我不知道你的父母是如何抚养你的,但他们做得不对。