[英]Execute jquery functions in order
function fn_one() {
$('#search-city-form').trigger('click');
}
function fn_two() {
$("form input[name='tariffId']").val("137");
$('#search-city-form').trigger('click');
}
function fn_three() {
$("form input[name='tariffId']").val("138");
$('#search-city-form').trigger('click');
}
function fn_four() {
$("form input[name='tariffId']").val("139");
$('#search-city-form').trigger('click');
}
$(document).on('click','.ui-menu li',function(){
fn_one();
fn_two();
fn_three();
fn_four();
});
I have this js code. 我有这个js代码。 I need to execute the functions in on(click) in order, ie
fn_one()
goes first, fn_two()
second and etc. 我需要按顺序执行on(click)中的函数,即
fn_one()
首先, fn_two()
第二等等。
Please give simple example of how can I achieve this. 请举一个简单的示例,说明如何实现此目标。
Edit: OK, I used the next function to be executed in the previous one but still I was getting erraneous data. 编辑:好的,我使用了下一个要在上一个函数中执行的函数,但是仍然获取错误的数据。 So I think I realized why this is happening.
所以我想我知道为什么会这样。 I will explain what
$('#search-city-form').trigger('click');
我将解释什么
$('#search-city-form').trigger('click');
does. 确实。 It triggers a form submit which makes an Ajax call and other function are not waiting till ajax request is complete.
它触发一个表单提交,该表单提交使Ajax调用和其他功能在ajax请求完成之前不等待。 So how do I make it to wait for ajax request to complete?
那么如何使它等待ajax请求完成呢?
EDIT 2: Event handler for $('#search-city-form')
: 编辑2:
$('#search-city-form')
事件处理程序:
$('#cdek').submit(function() {
var formData = form2js('cdek', '.', true, function(node) {
if(node.id && node.id.match(/callbackTest/)) {
return {
name : node.id,
value : node.innerHTML
};
}
});
var formDataJson = JSON.stringify(formData);
// console.log(JSON.stringify(formData));
document.getElementById('testArea').innerHTML = 'Отправляемые данные: <br />' + JSON.stringify(formData, null, '\t');
$.ajax({
url : 'http://api.cdek.ru/calculator/calculate_price_by_jsonp.php',
jsonp : 'callback',
data : {
"json" : formDataJson
},
type : 'GET',
dataType : "jsonp",
success : function(data) {
console.log(data.result.price);
} else {
for(var key in data["error"]) {
console.log(data["error"][key]);
}
}
}
});
return false;
});
I'll do this. 我会做的 If you need validate when the function has triggered, maybe you need to work with promises.
如果您需要在函数触发时进行验证,则可能需要兑现承诺。
function fn_one() { $('#search-city-form').trigger('click'); fn_two() } function fn_two() { $("form input[name='tariffId']").val("137"); $('#search-city-form').trigger('click'); fn_three() } function fn_three() { $("form input[name='tariffId']").val("138"); $('#search-city-form').trigger('click'); fn_four() } function fn_four() { $("form input[name='tariffId']").val("139"); $('#search-city-form').trigger('click'); } $(document).on('click','.ui-menu li',function(){ fn_one(); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.