javascript - 如何计算对话框中文本区域的字符数?

标签 javascript jquery html oracle-apex

我有以下代码:

<script type="text/javascript">                   
function createDialog(text, id) {
    return $("<div class='dialog'><textarea id='textarea' class ='texbox' name='textarea' value='text'>" + text + "</textarea></div>"
      .dialog({
        dialogClass: "dialogStyle",
        title: "Edit Description",
        resizable: false,
        position: {
          my: "right+240 top-200",
          at: "center",
          of: $("body"),
          within: $("body")
        },
        height: 200,
        width: 300,
        modal: true,
        buttons: {
          "Save": function() {
            var product = $(this).find('textarea [name="textarea"]').val();
            $(this).dialog("close");
            $("#" + id).val(product);
          },
          Cancel: function() {
            $(this).dialog("close");
          }
        },
        overlay: {
          opacity: 0.5,
          background: "black"
        }
      });
    }
</script>

如何在对话框中的文本区域中合并最多 255 个字符的字符计数? 我已经四处寻找代码,但将其放在函数 createDialog 中将不起作用,并且将其放入对话框中时获取长度变量也不起作用。

最佳答案

更改对话框以包含计数的 DIV:

    return $("<div class='dialog'><textarea id='textarea' class ='texbox' name='textarea' value='text'>" + text + "</textarea><div>Characters: <span class='charcount'>0</span></div></div>"

然后添加以下JS:

$(document).on("keyup edit paste", ".dialog .texbox", function(e) {
    var charcount = $(this).val().length;
    if (charcount > 255) {
        e.preventDefault();
        return;
    }
    $(this).closest(".dialog").find(".charcount").text(charcount);
});

function createDialog(text, id) {
  return $("<div class='dialog'><textarea id='textarea' class ='texbox' name='textarea' value='text' placeholder='"+text+"'></textarea><div>Characters: <span class='charcount'>0</span></div></div>")
    .dialog({
      dialogClass: "dialogStyle",
      title: "Edit Description",
      resizable: false,
      position: {
        my: "right+240 top-200",
        at: "center",
        of: $("body"),
        within: $("body")
      },
      height: 200,
      width: 300,
      modal: true,
      buttons: {
        "Save": function() {
          var product = $(this).find('textarea[name="textarea"]').val();
          $(this).dialog("close");
          $("#" + id).val(product);
        },
        Cancel: function() {
          $(this).dialog("close");
        }
      },
      overlay: {
        opacity: 0.5,
        background: "black"
      }
    });

}

$("#button").click(function() {
    createDialog("This is a message", "foo");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
<button id="button">Show dialog</button>
<input id="foo"/>

关于javascript - 如何计算对话框中文本区域的字符数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27611610/

相关文章:

jQuery 获取动态 id 链接的 id

html - 缩放图像以匹配段落高度

php - 使用 AJAX 接收选项时自动从 select 中选择一个选项

html - 如何为移动设备拆分/缩小表格

javascript - 长轮询 jQuery 在 IE 中不起作用

javascript - 一行导入多个组件(VueJs)

javascript - 选中列表复选框中的至少 1 个复选框时显示按钮

javascript - 将具有循环引用的对象从服务器传递到客户端 Javascript,同时保持循环性

javascript - 当滚动触发时,JQuery 多次重复百分号

jquery - 如何将嵌套的 div 向上移动两个 div 级别以匹配父级?