How can I prevent this jQuery function from being executed on every page load?

我只是一个虾纸丫 提交于 2019-12-01 13:08:49

The key is here:

$(function login() {
});

Remember that jQuery is not a different language - it's just javascript with some helper functions already defined. So you're calling the $() function, and passing in a function as an argument. The fact that you're naming the function is irrelevant - it's still getting passed as a parameter to $().

When you call the $() or jQuery() function and pass in another function, it's a shortcut for defining a document-ready event handler. So your code is really the same as doing this:

$(document).ready(function() {
    var formData = $('#login-form').serialize();
    $("input").prop("disabled", true);
    // the rest of your function here...
});

...which causes it to run on every page load.

If you're just defining a function that will be called later, there's no reason to wrap it in $(). You can define your function like this instead:

function login() {
    var formData = $('#login-form').serialize();
    $("input").prop("disabled", true);
    // the rest of your function here...
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!