简体   繁体   English

TinyMCE与jQuery验证插件

[英]TinyMCE with jQuery validation plugin

I am trying to use the jQuery validation plugin with tinyMce but I am having a small problem. 我正在尝试将jQuery验证插件与tinyMce一起使用,但是我遇到了一个小问题。

By default the validation plugin has the following behavior: initially every field is marked as valid and the validation is lazy ie the error isn't shown until the user enters a value and moves away from the field. 默认情况下,验证插件具有以下行为:最初,每个字段都被标记为有效,并且验证是惰性的,即直到用户输入一个值并离开该字段才显示错误。 Once a field is marked as invalid, it is eagerly validated which means the validation is done on every key stroke. 一旦将一个字段标记为无效,便会对其进行热切验证,这意味着将在每个按键上进行验证。

I am having difficulty simulating this behavior for the TinyMCE field. 我很难为TinyMCE字段模拟这种行为。 If I use the onChange method the validation is always done on focus out. 如果我使用onChange方法,则验证始终集中在焦点上。 If I use the onKeyDown method, validation is done on every key stroke, even if the user is changing the field for the first time. 如果我使用onKeyDown方法,则即使用户第一次更改该字段,也将在每个按键上进行验证。

Is there some way I can use a combination of onChange and onKeyDown in order to mimic the default behavior of jQuery validation plugin? 有什么办法可以结合使用onChange和onKeyDown来模仿jQuery验证插件的默认行为? Here is the code that I currently have: 这是我目前拥有的代码:

function(ed) {
    ed.onChange.add(function(ed, e) {
        tinyMCE.triggerSave();
        jQuery('#'+ed.id).valid();
    });
}

In case I am not making sense you can read how the validation plugin behaves here . 万一我没有意义,您可以在此处阅读验证插件的行为。

Thanks in advance! 提前致谢!

I think you're going to have to have a valid state variable 我认为您将必须具有有效的状态变量

Something like this 像这样

function(ed) {
    var validState = true;

    ed.onKeyDown.add(function(ed, e) {
        if (validState) {
            return
        }
        else {
            // check if new content is valid
            validState = jQuery('#'+ed.id).valid();
        }
    }

    ed.onChange.add(function(ed, e) {
        validState = jQuery('#'+ed.id).valid();

        if (validState){
            tinyMCE.triggerSave();
        }

    });
}

Note this is just some sample code. 请注意,这只是一些示例代码。 I haven't verified that this will work 我尚未验证这是否有效

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM