javascript 使用不显眼的验证插件时,如何禁用对 1 个特定 html 元素的 keyup 和 focusout 的 jquery 验证?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21450350/
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 disable jquery validation on keyup and focusout for 1 specific html element when using the unobtrusive validation plugin?
提问by TweeZz
By default, the jQuery validation pluginis attaching validation handlers for focusin
, focusout
and keyup
events.
默认情况下,jQuery 验证插件为focusin
、focusout
和keyup
事件附加验证处理程序。
1 of our validations is making a (synchronous) request to retrieve some data. I want to have that validation triggered only when the form is being submitted and not while the user is typing.
我们的验证之一是发出(同步)请求以检索一些数据。我希望仅在提交表单时触发该验证,而不是在用户输入时触发。
I know this can be modified for the whole form, but that's not what I'm looking for.
Is there a way to dynamically disable keyup validation for 1 element?
有没有办法动态禁用 1 个元素的 keyup 验证?
Update 1:
I forgot to mention that I'm using unobtrusive validation. So I don't think the answer of @Mario Johnathan is not an option.
更新 1:
我忘了提及我正在使用不显眼的验证。所以我不认为@Mario Johnathan 的答案不是一个选择。
Update 2: I tried the following things ($element is the element on which I want to change the validation behavior):
更新 2:我尝试了以下操作($element 是我想要更改验证行为的元素):
$element.validate({focusout: false, keyup: false});
$element.keyup(function() { return false; });
$element.off('keyup');
$element.unbind('keyup');
$element.validate({focusout: false, keyup: false});
$element.keyup(function() { return false; });
$element.off('keyup');
$element.unbind('keyup');
采纳答案by Mink
I myself was in the same scenario. Below is the code which I used for it.
我自己也处于同样的情况。下面是我用于它的代码。
$.validator.setDefaults({
onkeyup: function () {
var originalKeyUp = $.validator.defaults.onkeyup;
var customKeyUp = function (element, event) {
if ($("#your_element")[0] === element) {
return false;
}
else {
return originalKeyUp.call(this, element, event);
}
}
return customKeyUp;
}()
});
Here what I have done is handle keyup events for that element so that validator doesn't do anything. For rest all elements the validator will kick into action. But the catch here is that particular element will have no validation on keyup at all.
在这里,我所做的是处理该元素的 keyup 事件,以便验证器不执行任何操作。对于其余的所有元素,验证器将开始行动。但这里的问题是特定元素根本不会对 keyup 进行验证。
回答by Mario Radomanana
Try overriding the onkeyup function and create an onkeyup variable that you can customize for each field
尝试覆盖 onkeyup 函数并创建一个可以为每个字段自定义的 onkeyup 变量
$(#signupForm).validate({
onkeyup: function(element) {
var element_id = $(element).attr('id');
if (this.settings.rules[element_id].onkeyup !== false) {
$.validator.defaults.onkeyup.apply(this, arguments);
}
},
rules: {
myfield: {
required: true,
onkeyup: false
}
}
});
回答by Juancarlos Rodríguez
You can use one of the validate options.
您可以使用其中一种验证选项。
var validator = $('#form_id').validate({
onfocusout: false,
rules: {...},
messages: {...}
});
回答by lookbadgers
Add a new event listener to the $element and prevent the event bubbling up the DOM tree using stopPropagation(), which will prevent any parent handlers (including validate) being notified.
向 $element 添加一个新的事件侦听器,并使用 stopPropagation() 防止事件在 DOM 树中冒泡,这将阻止通知任何父处理程序(包括验证)。
http://api.jquery.com/event.stopPropagation/
http://api.jquery.com/event.stopPropagation/
$element.on('keyup',function(e){
e.stopPropagation();
});
回答by Sebastian Xawery Wi?niowiecki
From other forum:
来自其他论坛:
This is still an issue (jQuery 1.7.2 and Validation 1.9.0). I've created a simple fix and will also submit a pull request.
To fix: In jquery.validate.js in the "element" function (line 361) add this if statement to the top of the function like so (leave everything else the same):
这仍然是一个问题(jQuery 1.7.2 和 Validation 1.9.0)。我已经创建了一个简单的修复程序,并将提交一个拉取请求。
修复:在 jquery.validate.js 中的“元素”函数(第 361 行)中,将此 if 语句添加到函数顶部,如下所示(保持其他所有内容相同):
element: function( element ) {
//if the element is set to be ignored, don't continue validation
if ( $(element).not(this.settings.ignore).length === 0 ) {
return;
}
element = this.validationTargetFor( this.clean( element ) );
this.lastElement = element;
this.prepareElement( element );
this.currentElements = $(element);
var result = this.check( element ) !== false;
if (result) {
delete this.invalid[element.name];
} else {
this.invalid[element.name] = true;
}
if ( !this.numberOfInvalids() ) {
// Hide error containers on last error
this.toHide = this.toHide.add( this.containers );
}
this.showErrors();
return result;
},
回答by TweeZz
Until now I was able to achieve what I need by changing the source code of the jQuery.validation
plugin.
到目前为止,我能够通过更改jQuery.validation
插件的源代码来实现我所需要的。
By passing in the event (if any) to the validation methods, I'm able to check if it was triggered from a focusout or a keyup.
通过将事件(如果有)传递给验证方法,我可以检查它是否是由焦点或键盘触发。
Not ideal, but working.
不理想,但工作。
function validateWithSomeLongTakingCallback(source, args, event) {
var shouldTriggerValidation = (!event || (event.type !== 'focusout' && event.type !== 'keyup'));
if (!shouldTriggerValidation) {
args.IsValid = true;
return;
}
...
}