【发布时间】:2012-11-12 22:28:51
【问题描述】:
假设我有一个自定义 AddMethod 到 jQuery Validate,例如:
$.validator.addMethod('min-length', function (val, element) {
// do stuff
// the error message here needs to be dynamic
}, 'The field cannot be less than than '
+ element.attr('data-min') + // it is within the closure, but it can't grab it
' length.');
我想不出一种方法来获取有问题的 element 变量,并从中获取任何值。我在这里错过了什么?
【问题讨论】:
-
From the documentation,
addMethod可以将函数作为第三个(即message)参数。看看当你传递一个函数时会发生什么,在它里面console.log(this)。this很有可能是您感兴趣的元素。或者,元素作为参数传入,就像您拥有的其他函数一样。
标签: javascript jquery jquery-validate