如何计算对话框中文本区域的字符数
How do you count characters of a textarea within a dialog?
我有以下代码:
<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"/>
您可能希望为此使用委派。 你可以做这样的事情:
$(document).on('input propertychange', '#textarea', function() {
var text = $('#textarea').val();
if (text.length > 255) {
$('#textarea').val(text.substring(0,255));
}
//Maybe some code to display a message to the user, or update a character count element or something?
});
相关文章:
- onkeyup无法动态创建多个文本区域
- 具有所有样式的文本正在复制到可编辑文本区域
- 在文本区域POST后解码JSON
- HTML文本区域文本到XML
- 阻止在文本区域/文本框中输入数字
- 使用 javascript 单击图片后更改<文本区域>文本
- 使用 javascript 检测文本区域/文本输入中的 HTML 标记,但它应该支持常见的>和<
- 在按 Tab 键时选择文本区域文本
- 切换文本区域文本,但保留对文本的更改
- 清除文本区域/文本框内容的按钮
- 加载页面时,聚焦并全选文本区域文本
- 如何使可编辑文本区域文本不可选择
- 我的文本区域文本是't用javascript将我想要的样式设置为CSS
- 使用jQuery将文本区域文本安全地存储在隐藏字段中,然后稍后在PHP中使用该文本
- 删除文本区域文本提交在新的选项卡
- 如何在添加新文本时保持文本区域文本静态
- 使用 jQuery 将文本区域文本设置为格式正确的 HTML 代码
- 为什么文本添加到区域文本是通过页面刷新,而不是直接刷新
- 使用 jquery 修改文本区域文本
- 如何使用Javascript在文本区域(文本框)中单击(光标闪烁)而不使用鼠标单击