我设法使这个小jquery函数计算在textarea字段中输入的单词数.
这是代码:
JQUERY:
$(document).ready(function()
{
var wordCounts = {};
$("#word_count").keyup(function() {
var matches = this.value.match(/\b/g);
wordCounts[this.id] = matches ? matches.length / 2 : 0;
var finalCount = 0;
$.each(wordCounts, function(k, v) {
finalCount += v;
});
$('#display_count').html(finalCount);
am_cal(finalCount);
}).keyup();
});
Run Code Online (Sandbox Code Playgroud)
这是html代码
<textarea name="txtScript" id="word_count" cols="1" rows="1"></textarea>
Total word Count : <span id="display_count">0</span> words.
Run Code Online (Sandbox Code Playgroud)
如何在其中进行修改以获得这样的输出
总字数:0字.剩下的话:200
当它达到200个单词时,它不允许在jquery中粘贴或在textarea字段中输入更多单词?即它应限制用户输入不超过200字的字.
请帮忙.
非常感谢提前.
编辑:此代码只需要修改,因为我非常了解插件,但它们可能会干扰主代码.
这是计算单词的脚本.
HTML 消息: 总字数:0字.
jQuery的
$(document).ready(function () {
$('#word_count').wordCount();
});
jQuery.fn.wordCount = function (params) {
var p = {
counterElement: "display_count"
};
var total_words;
if (params) {
jQuery.extend(p, params);
}
//for each keypress function on text areas
this.keypress(function () {
total_words = this.value.split(/[\s\.\?]+/).length;
jQuery('#' + p.counterElement).html(total_words);
});
};
Run Code Online (Sandbox Code Playgroud)
这是用于检查表单的php代码段
<?php
include_once("includes/form_functions.php");
$var1 = 0;
$var2 = 0;
$var3 = 0;
$var4 = 0;
if(isset($_POST['submit'])) // checks if submit button is clicked of form
{
if(isset($_POST['rdvar1']))
{
$var5 = $_POST["rdvar1"];
if($_POST["rdvar1"] == "RJ")
{
$var1 = 20;
$var2 = 15;
}
else
{
$var1 = 5;
$var2 = 10;
}
}
if(isset($_POST['rdvar3']))
{
$var3 = $_POST["rdvar3"];
}
if(isset($_POST['rdvar4']))
{
$var4 = $_POST["rdvar4"];
}
elseif()
{
// many else if validation goes here
}
else
{
// some more internal …Run Code Online (Sandbox Code Playgroud)