jQuery 中的无限循环

发布于 2024-10-08 02:37:44 字数 219 浏览 6 评论 0原文

我对 jQuery 有疑问,由于某种原因,该代码会无限循环:

$(document).ready(function () {
    function changeURL() {
        location.href = 'http://aaa.com';
    }

    $('#daysLeftSort').change(changeURL());
});

I have a problem with jQuery, for some reason, that code makes infinite loop:

$(document).ready(function () {
    function changeURL() {
        location.href = 'http://aaa.com';
    }

    $('#daysLeftSort').change(changeURL());
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

疾风者 2024-10-15 02:37:44

分配事件处理程序时,请直接分配函数引用,而不是函数的结果,因此省略()(它调用立即运行...重新加载页面),如下所示:

$('#daysLeftSort').change(changeURL);

When assigning an event handler, assign the function reference directly, not the result of the function, so leave off the () (which calls the function immediately...reloading the page), like this:

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