简体   繁体   English

如何将函数编写为事件处理程序和带参数的可调用函数?

[英]How do I write a function as both an event handler and a callable function that takes an argument?

Here's a function for use as an event handler that makes use of this : 这是一个用作事件处理程序的函数,它使用了this函数:

function validate() {
  if (this.val() == '') {
    return false;
  }
}

$(':input').change(validate);

Here's the same function rewritten to take an argument, so that I can call it explicitly: 这是为了获取参数而重写的相同函数,因此我可以显式调用它:

function validate(field) {
  if ($(field).val() == '') {
    return false;
  }
}

validate($('#customer_name'));

How can I rewrite my validate function to make it suitable for use as both an event handler, and as a standalone function for me to call? 我怎么可以重写我的validate功能,使其适合用作两个事件处理程序,并作为一个独立的功能给我打电话?

There are various ways to do this. 有多种方法可以做到这一点。 One is to use the second one taking the field as a parameter and set the event handler using a closure: 一种是使用第二个将字段作为参数并使用闭包设置事件处理程序:

function validate(field) {
  if ($(field).val() == '') {
    return false;
  }
}

// Use anonymous function to pass "this" to validate.
$(':input').change(function() { validate(this); });

// Unchanged.
validate($('#customer_name'));

Another way is to use the first form and use apply() to call it with an overridden this : 另一种方法是使用第一种形式和使用适用()与覆盖调用它this

function validate() {
  if ($(this).val() == '') {
    return false;
  }
}

// Unchanged.
$(':input').change(validate);

// Use `$(...)` as "this" when calling validate.
validate.apply($('#customer_name'));

Use a fallback to this if field is not given as an argument. 使用回退来this ,如果field不作为参数给出。

function validate(field) {
    return $(field || this).val() != '';
}

$(':input').change(validate); // using this context
validate($('#someInput'));    // using a parameter

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写从服务器端 JavaScript 调用的 Java 方法,该方法将 JS 回调函数作为参数? - How do I write a Java method, to be called from server side JavaScript, that takes a JS callback function as an argument? 如何编写一个称为countDots(x)的函数,该函数将字符串作为参数并返回包含的点数(。)。 - How do I write a function called countDots(x) that takes a string as an argument and returns the number of dots (.) it contains CKEditor5,如何在我的 javascript 函数中编写一个 Blur 事件处理程序 - CKEditor5, how do I write a Blur event handler in my javascript function 如何编写一个接受值数组并返回对象的函数 - How do I write a function that takes an array of values and returns an object 如何使用 React Hooks 编写删除处理程序 function? - How do I write a delete handler function with React Hooks? 如何使用参数将函数附加到事件处理程序 - How do I attach a function to an event handler with parameters 如何将上下文绑定到事件处理程序,以便将事件和一些参数传递给处理函数 - How do I bind context to event handler so event and a few more arguments passed to handler function 如何使用处理程序将参数传递给 function? - How can I pass an argument into a function with a handler? 如何在 JavaScript 中放置在事件处理程序中的函数中获取 this.value 作为参数? - How to get this.value as an argument in a function placed in an event handler in JavaScript? 如果在初始化函数中分配了事件处理程序,如何传递参数? - How to pass argument if event handler is assigned in initialization function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM