如何将参数传递给事件处理函数?这会在页面加载时运行该功能,这不是预期的效果。我需要这个例程“validateText”来运行几个不同的文本框,下拉组合。我可以重复使用“validateText”,而不是为每个文本/下拉组合创建一个?jQuery,我怎样才能传递参数给事件处理程序?
//add blur event handler to the textbox with jQuery when the page is finished loading
$(document).ready(function() {
$("#myTextbox").blur(validateText($("#myTextbox"), $("#Select1")));
})
function validateText(textbox, dropdown) {
var message = $("#message");
var isValid = false;
//get the value the user type in
var textboxValue = $(textbox).val();
//get the options from the lookup
var options = $("option", dropdown);
//loop through the options and compare it to "value"
options.each(function() {
var optValue = $(this).val();
if (optValue === textboxValue) {
isValid = true;
}
});
if (!isValid)
message.text(textboxValue + " is not a valid value from the list.");
else
message.text(textboxValue + " is perfectly valid.");
}
感谢布赖恩。这很好! – Hcabnettek 2009-08-04 22:55:42
从技术上讲,它正在执行的原因是因为当jQuery回调请求函数指针时传递函数调用 - 这就是匿名函数工作的原因。 – Nic 2009-08-12 06:16:31