jQuery 从 .each() 中的函数返回未定义的字符串

发布于 2025-01-04 04:48:22 字数 680 浏览 0 评论 0原文

我试图从名为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

源来凯始玺欢你 2025-01-11 04:48:22

这与同步调用有关吗?

是的。嗯,实际上它与异步调用有关。在 AJAX get 返回任何内容之前,您的 tjekdet 函数将返回 undefined

将警报放入成功回调中:

$.get("test2.php", { test: name }, function(data) {
    alert(data);
    //Do whatever you need to do here, not in the other function
});

您可以使用 jQuery.ajax 而不是 jQuery.get 并将 async 选项设置为 false,但这很少是一个好主意,并且往往会完全锁定浏览器,直到数据已返回。

Does this have something to do with synchronous calls?

Yes. Well, really it has to do with asynchronous calls. Your tjekdet function will return undefined before your AJAX get returns anything.

Put the alert inside the success callback:

$.get("test2.php", { test: name }, function(data) {
    alert(data);
    //Do whatever you need to do here, not in the other function
});

You could use jQuery.ajax instead of jQuery.get and set the async option to false, but that's rarely a good idea and tends to lock up the browser completely until the data has returned.

情话难免假 2025-01-11 04:48:22

get 是带有一组特定选项的 ajax 的简写。 AJAX 是异步的。这意味着当您的函数完成时请求尚未返回。

get 返回类似内容的唯一选择是使其成为同步调用,这在几乎所有情况下都是一个坏主意,因为浏览器在等待时会挂起以获得回应。

相反,您应该在回调中对请求的结果执行您需要执行的操作。

get is shorthand for ajax 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.

忆梦 2025-01-11 04:48:22

因为 tjekdet() 实际上并不返回任何内容。 tjekdet 中的 return 语句实际上属于匿名回调,与 tjekdet 的返回无关。

此外,tjekdet 内的回调是异步调用的,因此当您调用alert(tester); 时它可能还没有执行。

Because tjekdet() doesn't actually return anything. The return statement in tjekdet really belongs to the anonymous callback, and has nothing to do with the return of tjekdet.

Additionally, the callback inside tjekdet is invoked asynchronously, so it will probably not even have executed yet when you call alert(tester);.

完美的未来在梦里 2025-01-11 04:48:22

你的代码应该是这样的:

function tjekdet(name, callback) {
    $.get("test2.php", { test: name },
        function(data){
            callback(data);
    });
}

jQuery(function($) {
    $('.button').live("click",function() {
        $(".navn-list").each(function() {
            var navn = $(this).text();
            tjekdet(navn, function(data){
                alert(data);
            });
        });
    });
});

tjekdet 中,你的 ajax 调用是异步的,你可以在该调用完成时执行一些操作...但是由于 tjekdet 会立即返回,你不能指望从 tjekdet 获取正确的值,但您应该调用一些回调方法。

This is how your code should look like:

function tjekdet(name, callback) {
    $.get("test2.php", { test: name },
        function(data){
            callback(data);
    });
}

jQuery(function($) {
    $('.button').live("click",function() {
        $(".navn-list").each(function() {
            var navn = $(this).text();
            tjekdet(navn, function(data){
                alert(data);
            });
        });
    });
});

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文