jQuery 从 .each() 中的函数返回未定义的字符串
我试图从名为“tjekdet”的函数返回一个字符串,但它除了未定义之外不会返回任何内容。我只需要它返回字符串,这样我就可以将结果附加到 $(this) 中。
我这样做的原因是因为对 test2.php 的请求大约需要 40 秒,所以我宁愿一一执行。但没关系。
这与同步调用有关吗?或者我只是完全错过了一些真正基本的东西?
function tjekdet(name) {
$.get("test2.php", { test: name },
function(data){
// alert(data); returns correctly in the alert
return data;
});
}
jQuery(function($) {
$('.button').live("click",function() {
$(".navn-list").each(function() {
var navn = $(this).text();
var tester = tjekdet(navn);
alert(tester); // returns undefined
});
});
I'm trying to return a string from of function called 'tjekdet', but it won't return anything but undefined. I just need it to return the string, so I can append the result to $(this).
The reason for me to do it this way, is because the request for test2.php takes around 40 seconds, so I'd rather do them one by one instead. But nevermind that.
Does this have something to do with synchronous calls? Or am I just completely missing something really basic?
function tjekdet(name) {
$.get("test2.php", { test: name },
function(data){
// alert(data); returns correctly in the alert
return data;
});
}
jQuery(function($) {
$('.button').live("click",function() {
$(".navn-list").each(function() {
var navn = $(this).text();
var tester = tjekdet(navn);
alert(tester); // returns undefined
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的。嗯,实际上它与异步调用有关。在 AJAX
get
返回任何内容之前,您的tjekdet
函数将返回undefined
。将警报放入成功回调中:
您可以使用
jQuery.ajax
而不是jQuery.get
并将async
选项设置为false
,但这很少是一个好主意,并且往往会完全锁定浏览器,直到数据已返回。Yes. Well, really it has to do with asynchronous calls. Your
tjekdet
function will returnundefined
before your AJAXget
returns anything.Put the alert inside the success callback:
You could use
jQuery.ajax
instead ofjQuery.get
and set theasync
option tofalse
, but that's rarely a good idea and tends to lock up the browser completely until the data has returned.get
是带有一组特定选项的ajax
的简写。 AJAX 是异步的。这意味着当您的函数完成时请求尚未返回。从
get
返回类似内容的唯一选择是使其成为同步调用,这在几乎所有情况下都是一个坏主意,因为浏览器在等待时会挂起以获得回应。相反,您应该在回调中对请求的结果执行您需要执行的操作。
get
is shorthand forajax
with a certain set of options. AJAX is asynchronous. This means that the request has not yet returned when your function completes.The only option to return something like that from a
get
is to make it a synchronous call, which is a bad idea in almost every case, as the browser will hang while waiting for a response.You should instead do whatever it is you need to do with the result of the request in your callback.
因为
tjekdet()
实际上并不返回任何内容。 tjekdet 中的 return 语句实际上属于匿名回调,与 tjekdet 的返回无关。此外,tjekdet 内的回调是异步调用的,因此当您调用alert(tester); 时它可能还没有执行。
Because
tjekdet()
doesn't actually return anything. The return statement intjekdet
really belongs to the anonymous callback, and has nothing to do with the return oftjekdet
.Additionally, the callback inside
tjekdet
is invoked asynchronously, so it will probably not even have executed yet when you callalert(tester);
.你的代码应该是这样的:
在
tjekdet
中,你的 ajax 调用是异步的,你可以在该调用完成时执行一些操作...但是由于 tjekdet 会立即返回,你不能指望从 tjekdet 获取正确的值,但您应该调用一些回调方法。This is how your code should look like:
inside
tjekdet
your ajax call is async, and you can execute some action when that call is completed...but since tjekdet will return right away, you cannot expect to get correct value from tjekdet, but you should call some callback method instead.