jQuery 如何在jquery中定义函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1928949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to define functions in jquery?
提问by Mike
I've the code below that works great in my ready function in jquery, when i fill the form and change focus from the input element to another run the ajax check and assign a css class to the element, showing if is validated or not. everything ok.
我下面的代码在 jquery 中的就绪函数中非常有效,当我填写表单并将焦点从输入元素更改为另一个时,运行 ajax 检查并为元素分配一个 css 类,显示是否已验证。一切还好。
id' like to define with this code a function so i can call it also when the page is just loaded, so the check will be performed also if the form is populated in server side, and on blur, but i faced troubles defining functions in jquery.
id' 喜欢用这段代码定义一个函数,这样我也可以在页面刚加载时调用它,所以如果表单在服务器端填充和模糊,也会执行检查,但我在定义函数时遇到了麻烦查询。
this is the code that runs on blur
这是在模糊上运行的代码
$('#join_username').blur(function(){
$('#join_username').addClass(\"join_form_input_checking\");
$('#join_username').removeClass(\"join_form_input\");
$('#join_username').removeClass(\"join_form_input_error\");
$('#join_username').removeClass(\"join_form_input_verified\");
$.post(\"inc/viewlets/join_form/check_username.php\", {
join_username: $('#join_username').val()
},
function(response){
setTimeout(\"finishAjaxUsername('usernameResult', '\"+escape(response)+\"')\", 400);
if(escape(response)=='true') joinFormCheck[0]=true;
else joinFormCheck[0]=false;
checkFormArray(joinFormCheck);
}
);
return false;
});
回答by George Godik
or you could use use regular function syntax
或者您可以使用使用常规函数语法
function do_on_blur(){
// your base. I'm in it
}
and
和
$("#join_username").blur( do_on_blur );
回答by typeoneerror
Simply define a function instead of passing the blur()
an anonymous function:
只需定义一个函数而不是传递blur()
匿名函数:
$.myFunctionName = function()
{
// your code here
}
$("#join_username").blur($.myFunctionName);
If you want the function to be called in response to an event (such as document.ready), just call it like you would a normal function.
如果您希望在响应事件(例如 document.ready)时调用该函数,只需像调用普通函数一样调用它即可。
$(document).ready(function()
{
// -- define your function
$.myFunctionName = function()
{
//your code here
}
// -- add a callback to blur on an input
$("#join_username").blur($.myFunctionName);
// -- manually call the function once
$.myFunctionName();
});
回答by rfunduk
I think you just have invalid syntax here:
我认为您在这里的语法无效:
$('#join_username').blur( function(){
$(this)
.addClass("join_form_input_checking")
.removeClass("join_form_input");
.removeClass("join_form_input_error");
.removeClass("join_form_input_verified");
$.post("inc/viewlets/join_form/check_username.php", {
join_username: $(this).val()
}, function(response) {
setTimeout( function() {
finishAjaxUsername('usernameResult', escape(response))
}, 400);
if(escape(response)=='true') { joinFormCheck[0]=true; }
else { joinFormCheck[0]=false; }
checkFormArray(joinFormCheck);
} );
return false;
} );
Try that. Then, of course if you want the function to be separate from the blur event you can simply assign it to a variable... so you'd end up with:
试试那个。然后,当然,如果您希望该函数与模糊事件分开,您可以简单地将其分配给一个变量……所以您最终会得到:
var blurFunc = function() { ...function from above... };
$('#join_username').blur( blurFunc );
回答by kiamlaluno
If you don't want to create a jQuery plugin, then it is enough to pass to that jQuery function a reference to the function you want to use. Anonymous functions are used when you need that function only once.
如果您不想创建 jQuery 插件,那么向该 jQuery 函数传递对您要使用的函数的引用就足够了。当您只需要一次该函数时,将使用匿名函数。
function blur() {};
$('#join_username').blur(blur);
That is normally done when the function is used more than once; normally, you will use an anonymous function.
这通常在函数被多次使用时完成;通常,您将使用匿名函数。