简体   繁体   English

X可编辑:验证失败时关闭编辑区域

[英]X-editable: close editing area on validation failure

What I have: 我有的:

Multiple comments in some page. 某些页面中有多个评论。 Each user can modify all his comments. 每个用户都可以修改其所有评论。

$.each(comments, function() {
    this.editable({
        type: 'textarea',
        mode: 'inline',
        onblur: 'submit',
        showbuttons: false,
        inputclass: 'edit-comments-text-input',
        validate: function(value) {
            if (!value.trim()) {
                return 'Can not be empty!'; //DON'T CLOSE THE EDITABLE AREA!!!
            }
        },
        success: function(response, newValue){        
            comment.text = newValue.trim();

    });
});

What a problem: 有什么问题:

If user input an empty string, validation fails. 如果用户输入空字符串,则验证失败。 BUT the editable area won't be closed, so user can move on and edit the next comment, with still open editing area for the previous. 但是可编辑区域不会关闭,因此用户可以继续并编辑下一个注释,而上一个注释的编辑区域仍处于打开状态。

Question: 题:

How to close editing text area on validation failure? 验证失败时如何关闭编辑文本区域?

I read some source code, and I got it sorted. 我读了一些源代码,然后整理了一下。

The important line is: 重要的一行是:

$('.editable-open').editableContainer('hide', 'cancel');

so in the example above this would be: 因此在上面的示例中将是:

$.each(comments, function() {
    this.editable({
        type: 'textarea',
        mode: 'inline',
        onblur: 'submit',
        showbuttons: false,
        inputclass: 'edit-comments-text-input',
        validate: function(value) {
            if (!value.trim()) {
                $('.editable-open').editableContainer('hide', 'cancel');
                return 'Can not be empty!'; //DOES NOW CLOSE THE EDITABLE AREA!!!
            }
        },
        success: function(response, newValue){        
            comment.text = newValue.trim();

    });
});

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

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