我有一个元素。
我想执行另一个函数,但在该函数中,我希望等待直到属性
<span id="current" data=""></span>
data
属性的值将由一个异步的AJAX函数填充。我不能把这个函数改成同步的,也不能让它返回一个值。
以下是代码的一部分:
$("#popup #save").click(function () {
//setting a lot of vars here
var id = $("#current").val();
if (id == '') {
//new so insert
$.ajax({
type: "GET",
url: myurl,
data: "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
$("#loadstatus").html('');
//returns new id
if (data > 0) {
$('#current').val(data);
}
}
});
}
else {
//existing so update
//more code
}
return false;
});
var id = $("#current").val();
if (id == '') {
//save
$("#popup #save").click(); //this function contains the AJAX call which also sets `$("#current").val()` with a value returned in that same AJAX call
//I can only set the value of id after the success of the aforementioned AJAX function
//so here I need to set some sort of timeout
id = $("#current").val();
}
我想执行另一个函数,但在该函数中,我希望等待直到属性
data
不为空。
我查看了这个链接:http://javascriptisawesome.blogspot.com/2011/07/faster-than-jquerydocumentready-wait.html
但我更喜欢使用默认的jQuery实现。
我该如何做?
$.ajax({ type: "GET"
。 - Adam$.ajax(...)
代码块。 - Ja͢ck