【问题标题】:using tinymce 4.0 text editor how to limit character?使用 tinymce 4.0 文本编辑器如何限制字符?
【发布时间】:2014-09-27 16:17:13
【问题描述】:

这是我的 tinymce 脚本:

<script>
      tinymce.init({
          menubar: false,
          selector: "textarea",
          plugins: [
              "advlist autolink lists link image charmap print preview anchor",
              "searchreplace visualblocks code fullscreen",
              "insertdatetime media table contextmenu paste"
          ],
          toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
      });
    </script>

如何将字符限制为 500 个?

【问题讨论】:

标签: javascript tinymce


【解决方案1】:

我添加了“stopPropagation”行,以防止用户继续输入字符: 1 - 在 textarea 的 html 代码中,必须包含 maxlength 和 Id 的值。
2 - 在脚本部分,代码如下。如果需要,请取消注释 alert() 行,然后输入您的消息。

<script type="text/javascript">
  tinymce.init ({
    ...
    ...
      setup: function(ed) {
        var maxlength = parseInt($("#" + (ed.id)).attr("maxlength"));
        var count = 0;
        ed.on("keydown", function(e) {
          count++;
          if (count > maxlength) {
            // alert("You have reached the character limit");
            e.stopPropagation();
            return false;
          }
        });
     },
&lt;textarea type="text" id="test" name="test" maxlength="10"&gt;&lt;/textarea&gt;

【讨论】:

    【解决方案2】:

    它可以工作,但我不知道如何让它可以按删除键...

      setup: function(ed) {
            var maxlength = parseInt($("#" + (ed.id)).attr("maxlength"));
            var count = 0;
            ed.on("keydown", function(e) {
                count++;
                if (count >= maxlength) {
                    alert("false");
                    return false;
                }
            });
        },
    

    【讨论】:

      猜你喜欢
      • 2012-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多