请帮帮我...
我必须将值传递给php文件进行保存,但是如果选中了复选框,则必须使用第一个返回值执行第二个jquery post。但似乎第一个jquery帖子首先被执行,这是我的代码:
if($action=="save_prod")
{
var remember = document.getElementById('chk');
var $suppid=document.getElementById('supp').value;
var $categlist=document.getElementById('categlist').value;
var $prodname=document.getElementById('prodname').value;
var $proddesc=document.getElementById('proddesc').value;
$.post(baseurl+'index.php/main/newproduct',{"categlist" : $categlist,"prodname" : $prodname," proddesc" : $proddesc,"supp" : $suppid},function(response)
{
$lastid=response;
if($lastid != 0)
{
alert("Product has been saved with ID=" + $lastid);
document.getElementById('resp').style.display='none';
document.getElementById('fade').style.display='none';
window.location.href=baseurl +"index.php/main/mgtprods/?key="+ $suppid;
}
});
if (remember.checked)
{
//alert($lastid);
$.post(baseurl+'index.php/main/newitem',{"prodlist" : $lastid ,"itlist" : 0,"itcost" : 0, "supp" : $suppid, "itname" : $prodname, "itunit" : "pc" },function(response)
{
document.getElementById('resp').style.display='none';
document.getElementById('fade').style.display='none';
//window.location.href=baseurl +"index.php/main/mgtprods/?key="+ $suppid;
});
}
}
非常感谢你!
答案 0 :(得分:2)
jQuery ajax函数($.ajax()
,$.get()
,$.post()
...)都返回一个包裹底层jqXHR
对象的Deferred对象。
利用Deferred.then()
函数链接两个异步调用:
//your first $.post :
$.post(...)
.then(function(){
if (remember.checked) {
//your second $.post :
$.post(...);
}
});
以这种方式使用它,只有在第一次调用成功时才会调用第二个函数(如果第一次调用导致错误,则不会触发任何内容)。
您的第一个回调包含重定向:window.location.href=baseurl +"index.php/main/mgtprods/?key="+ $suppid;
。当您想要执行此重定向时,您必须更精确地选择。
如果选中remember
,则在第二次调用后触发重定向,或者如果未检查remember
则在第一次调用后立即触发重定向,方法是链接Deferred
对象:
$.post(... /* remove the redirection from the first callback */)
.then(function(){
if (remember.checked) {
//your second $.post : return the Deferred corresponding to this call
return $.post(... /* remove the redirection from the second callback */);
} else {
// else, return the first Deferred object :
return this;
}
}).then(function(){
/* do your redirection here : */
window.location.href=baseurl +"index.php/main/mgtprods/?key="+ $suppid;
});
答案 1 :(得分:0)
试试这个......
if($action=="save_prod") {
var remember = document.getElementById('chk');
var $suppid=document.getElementById('supp').value;
var $categlist=document.getElementById('categlist').value;
var $prodname=document.getElementById('prodname').value;
var $proddesc=document.getElementById('proddesc').value;
$.post(baseurl+'index.php/main/newproduct',{"categlist" : $categlist,"prodname" : $prodname," proddesc" : $proddesc,"supp" : $suppid},function(response) {
$lastid=response;
if (remember.checked) {
//alert($lastid);
$.post(baseurl+'index.php/main/newitem',{"prodlist" : $lastid ,"itlist" : 0,"itcost" : 0, "supp" : $suppid, "itname" : $prodname, "itunit" : "pc" },function(response) {
document.getElementById('resp').style.display='none';
document.getElementById('fade').style.display='none';
//window.location.href=baseurl +"index.php/main/mgtprods/?key="+ $suppid;
});
}
if($lastid != 0) {
alert("Product has been saved with ID=" + $lastid);
document.getElementById('resp').style.display='none';
document.getElementById('fade').style.display='none';
window.location.href=baseurl +"index.php/main/mgtprods/?key="+ $suppid;
}
});
}
我所做的就是将第二篇文章移到第一篇文章的回调函数中。您也可以在第一篇文章中添加.done()
方法,然后将第二篇添加到该帖子中,但效果相同。