【问题标题】:Wysihtml5 editor - Limit number of charactersWysihtml5 编辑器 - 限制字符数
【发布时间】:2016-06-29 20:39:58
【问题描述】:

我有这个:

<div class="row">
    <div class="col-md-11">
        <label for="" class="control-label">Description<span>*</span></label>
        @Html.TextAreaFor(m => m.Description, new { @class = "editor", @maxlength = "500" })<br />
        @Html.ValidationMessageFor(model => model.Description)
     </div>
</div>

以及 wysihtml5 编辑器的 js(使用 Razor 引擎):

<script type="text/javascript">
    $('.editor').wysihtml5({
        stylesheets: "/Content/css/wysiwyg-color.css",
        charset: "utf-8"
    })
</script>

所以基本上我要做的是限制此字段中的字符数(最大 500 个字符)。如您所见,我在@Html.TextAreaFor 中设置了maxlength 属性,但没有成功。

有人可以告诉我如何做到这一点吗?提前致谢。

【问题讨论】:

    标签: javascript c# jquery iframe wysihtml5


    【解决方案1】:

    为您的文本区域使用一个 ID,而不是一个类。
    Wysihtml5 使用此 ID 创建一个 iframe。

    它用于 iframe 的 id 是 "[your textarea id]-sandbox"
    由于这个元素是动态创建的,我更新了下面的代码(这是一个编辑过的答案)

    $(document).ready(function{
        var PreventSubmitFlag=false;
    
        $('#editor').sibling("#editor-sandbox").find("body").on("keyup", function(e){
            var content = $(this).html();
            if(content.length>500){
                alert ("500 characters is the limit!");
                PreventSubmitFlag=true;
            }else{
                PreventSubmitFlag=false;
            }
        });
    });
    

    使用PreventSubmitFlag 禁用提交按钮。 ;)

    【讨论】:

    • 它似乎没有正常工作,警报弹出窗口甚至没有出现。我尝试将editor-sandbox 更改为wysihtml5-sandboxeditor.wysihtml5-sandbox,但什么也没发生。我似乎无法弄清楚为什么......
    猜你喜欢
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多