jquery 验证 onchange

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16914790/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:02:50  来源:igfitidea点击:

jquery validate onchange

jqueryvalidationjquery-validateonchange

提问by Snafu

I'm using http://jqueryvalidation.org/plugin.

我正在使用http://jqueryvalidation.org/插件。

It validate on key up but no on change. For example:

它在启动时验证,但在更改时不验证。例如:

<input type="text" id="test1" value="" name="test1">
<input type="checkbox" id="test2" name="test2" value="1"> 

jQuery('#test2').change(function() {
jQuery('#test1').val('Test1'); });

if I'm validating, validate error at #test1 shows, but when i click checkbox, it's not hidding.

如果我正在验证,#test1 显示验证错误,但是当我单击复选框时,它没有隐藏。

I can do it in this way:

我可以这样做:

jQuery('#test2').change(function() {
jQuery('#test1').val('Test1');
jQuery('#test1').valid(); });

it works, but how to implement onchange method in validation plugin, for automatic, every field onchange validation? Not only onkeyup, but also onchange?

它有效,但是如何在验证插件中实现 onchange 方法,以实现自动的每个字段 onchange 验证?不仅onkeyup,还有onchange?

回答by ArdaTahsinAyar

Start using 'input' event instead of 'change'.

开始使用 'input' 事件而不是 'change'。

jQuery('#test2').on('input', function() {
    // do your stuff 
});

回答by Rohan Kumar

Try

尝试

jQuery('#test2').on('click',function() {
    $(this).validate({
       // your code here
    });
});

Read jQuery Validator -- only validate onblur event (not onchange)

读取jQuery Validator -- 只验证 onblur 事件(不是 onchange)