使用字数统计选项在文本区域中编辑 PHP 查询结果



>我已经在我的表单中创建了一个字数统计文本区域,参考您的网站,它工作正常。我正在尝试在下面的代码中编辑我的 mysql 数据库中的数据,我可以在文本区域中获取数据,但字数统计不起作用。

有人帮我吗?

$(document).ready(function() {
$("#12").on('keyup', function() {
var words = this.value.match(/S+/g).length;
if (words > 300) {
// Split the string on first 300 words and rejoin on spaces
var trimmed = $(this).val().split(/s+/, 300).join(" ");
// Add a space at the end to keep new typing making new words
$(this).val(trimmed + " ");
}
else {
$('#display_count_12').text(words);
$('#word_left_12').text(300-words);
}
});
});
<textarea name="f12" id="12" style="width:753px; height:80px;"><?php echo $f12;?></textarea><br>
<span style="font-size:14px"> &nbsp; Total word count: <span id="display_count_12">0</span> words. Words left: <span id="word_left_12" style="font-size:14px;color:red">300</span></span>

这是因为字数仅在您在文本区域中键入内容时才更新。
因此,您必须在页面加载时启动它。

用它替换你的 js。

$(document).ready(function() {
$(function(){
let words = $('#12').val().match(/S+/g).length;
$('#display_count_12').text(words);
$('#word_left_12').text(300 - words);
});
$("#12").on('keyup', function() {
var words = this.value.match(/S+/g).length;
if (words > 300) {
// Split the string on first 300 words and rejoin on spaces
var trimmed = $(this).val().split(/s+/, 300).join(" ");
// Add a space at the end to keep new typing making new words
$(this).val(trimmed + " ");
}
else {
$('#display_count_12').text(words);
$('#word_left_12').text(300-words);
}
});
});

最新更新