如何在Summernotes中禁用编辑?

问题描述 投票:11回答:3

我正在使用summernotes组件,我想要做的是我想阻止用户添加字符,如果s(他)超过2000 Chars但我无法弄清楚如何停止输入事件。

我正在做的是如下:

 $(".note-editable").each(function(){
           $(this).on("keyup", function(){
                var cc = $(this).text().length;
                var arr = $(this).next().attr("class").split('_');
                var q = "q"+arr[0];
                var maxChar = arr[2];
                var textarea = $('*[name^="'+q+'"]');
                var diffChar = parseInt(maxChar - cc);

                if(diffChar >= 0)
                {
                    $(this).next().text(diffChar + " Remaining out of " + maxChar);
                }
                else
                {
                    $(this).next().text("0 Remaining out of " + maxChar);

                    //$(textarea).prop('disabled', true);
                    $(this).text($(this).text().slice(0,diffChar));
                }
           });
        });

任何想法如何做到这一点,我不想禁用光标或破坏summernote ..我想让用户觉得他仍然可以编辑,但如果文本超过2000个字符,它将不会输入任何内容。

谢谢!!

jquery summernote
3个回答
8
投票

从官方文件中查看this link

基本上,您需要做的是:

您可以通过API禁用编辑器。

$('#summernote').summernote('disable');

如果要再次启用编辑器,请使用enable调用API。

$('#summernote').summernote('enable');

现在,您可以做的是,在您自己的代码逻辑/算法中使用这些API调用来获得所需的效果。

我知道这是一个老问题,但希望这会有所帮助。


3
投票

我知道这是一个老问题,但您可以使用容器查找下一个'.note-editable'并将其'contenteditable'属性设置为false。为我工作。

$('#summernote').next().find(".note-editable").attr("contenteditable", false);

0
投票

您应该使用内置方法来执行此操作:

$("#target").summernote("disable");

要重新启用它,请使用:

$("#target").summernote("enable");

但是,禁用版本也会禁用代码视图按钮,这对我来说没有意义。为什么用户无法查看(不编辑)代码?我们谈论的是禁用版本,与代码无关。

所以这是我的解决方法:

function disableSN(){
    $("#target").summernote("disable");

    // Enables code button:
    $(".note-btn.btn-codeview").removeAttr("disabled").removeClass("disabled");

    // When switching from code to rich, toolbar buttons are clickable again, so we'll need to disable again in that case:
    $(".note-btn.btn-codeview").on("click", codeViewClick);

    // Disables code textarea:
    $("textarea.note-codable").attr("disabled", "disabled");
}

function enableSN(){
    // Re-enables edition and unbinds codeview button eventhandler
    $("#target").summernote('enable');
    $(".note-btn.btn-codeview").off("click", codeViewClick);
}

function codeViewClick(){
    // If switching from code view to rich text, disables again the widget:
    if(!$(this).is(".active")){
        $("#target").summernote("disable");
        $(".note-btn.btn-codeview").removeAttr("disabled").removeClass("disabled");
    }
}

请使用summernote小部件附加的代码替换上面代码中的#target选择器。

© www.soinside.com 2019 - 2024. All rights reserved.