如何在jquery ajax调用完成后延迟默认表单动作
How to postpone default form action after jquery ajax call finishes?
我有这样一个表单:
<form class='setPricetInput' method='post' action='mos.php'>
<label>
<span class = 'boldFont'>Your Selling Price:</span>
<input type='text' class='moneyFormat reqPrice' autocomplete='off' name='b2'>
</label>
<button class='setPriceButton'>set</button>
</form>
这个jquery点击函数:
$(".setPriceButton").click(function(){
$this = $(this);
var $thisForm = $this.parent();
var $detailLine = $this.next(".detailLine");
var buyOrderID = $detailLine.find('.orderID');
var priceSet =ParseFloat($thisForm.find('.reqPrice').val().replace('$','')).toFixed(2);
var sendData = {'value': buyOrderID, 'type': 'Buy_Order_ID'};
$.getJSON('addToSession.php',sendData);
var sendData = {'value': priceSet, 'type': 'Price_Set'};
$.getJSON('addToSession.php',sendData);
})
我希望默认的表单操作(most .php)后运行的jquery点击功能。jquery代码将值添加到_SESSION中,这是摩斯.php正确运行所必需的。如何将默认动作推迟到jquery函数完成后?
要在表单上运行默认提交,您需要运行.submit()
。通常我们会把它放在回调中,但是因为你有2个ajax调用在运行,我们需要确保和在我们提交表单之前都完成了。为此,我们在.click
作用域之外创建一个变量和一个函数。接下来,我们创建一个函数,它将在每个$.getJSON
完成时运行。现在,该函数第一次运行时,它将检查firstFinished
是否设置为true
,如果不是,则将其设置为true
,以便第二次调用将提交表单。这样,如果第一个$.getJSON
请求在第二个请求之后返回,您将没有问题。
var firstFinished = false,
getJSONDone = function(){
if(!firstFinished){
firstFinished = true;
return false;
}
$('.setPricetInput').submit();
};
$(".setPriceButton").click(function(e){
e.preventDefault();
$this = $(this);
var $thisForm = $this.parent();
var $detailLine = $this.next(".detailLine");
var buyOrderID = $detailLine.find('.orderID');
var priceSet =ParseFloat($thisForm.find('.reqPrice').val().replace('$','')).toFixed(2);
var sendData = {'value': buyOrderID, 'type': 'Buy_Order_ID'};
$.getJSON('addToSession.php',sendData, getJSONDone);
var sendData = {'value': priceSet, 'type': 'Price_Set'};
$.getJSON('addToSession.php',sendData, getJSONDone);
});
使用$.getJSON()
回调函数提交表单
$.getJSON('addToSession.php',sendData,function()
{
//call this another one
$.getJSON('url','data',function()
{
//in the last,make form submit.
$( ".setPricetInput" ).submit();
}
);
});
http://api.jquery.com/jquery.getjson/http://api.jquery.com/submit/相关文章:
- 用嵌套函数和默认函数定义函数
- 可以简化嵌套的延迟Q Promises解析吗
- 如何使用铁流星与流星的默认路线
- 防止Alt+Shift默认操作或检测多种操作系统语言的Javascript
- CKeditor:更改对话框中的默认选择选项
- 在不阻止默认行为的情况下检测IE10中的缩放
- onclick函数需要双击,因为类分配延迟
- 如何从外部页面激活非默认引导选项卡
- 防止默认锚点行为AngularJS
- 我的shareService在angular 2中发送值工作正常,但当我渲染我的另一个组件时,会获得默认值
- jpm的默认Firefox路径没有'不起作用
- 更多延迟动画
- jquerydatetimepicker日期和时间应在框默认值中,而无需单击框
- 如果值为空,如何设置输入的默认值?jQuery
- 骨干模型默认值-todos.js示例中不必要的代码
- jquery日期选择器年份范围默认值
- 默认情况下,浏览器延迟加载悬停图像
- 图像的默认灯箱在使用延迟加载时被禁用
- 如何在jquery ajax调用完成后延迟默认表单动作
- clientWidth和clienttheight在Android默认浏览器上延迟