对多个文本区域的字限制 [英] word limits on multiple text areas

查看:122
本文介绍了对多个文本区域的字限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个具有四个textarea表单的网站。每个表单都有字数限制。

I am creating a website with four textarea forms. Each form has a word limit.


  • textarea1:250个字数限制

  • textarea2:500个字数限制

  • textarea3:500字数限制

  • textarea4:250个字数限制

  • textarea1: 250 word limit
  • textarea2: 500 word limit
  • textarea3: 500 word limit
  • textarea4: 250 word limit

尝试使用我在尝试修复此问题时发现的现有示例,但似乎无效。

I have tried using existing examples that I have found when trying to fix this problem but nothing seems to work.

   <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script>
var maxwords = 250;
//
function check_length(obj, cnt, rem)
{
    var ary = obj.value.split(" "); // doubled spaces will throw this off
    var len = ary.length;
    cnt.innerHTML = len;
    rem.innerHTML = maxwords - len;
    if (len > maxwords) {
        alert("Message in '" + obj.name + "' limited to " + maxwords + " words.");
        ary = ary.slice(0,maxwords-1);
        obj.value = ary.join(" "); // truncate additional words
        cnt.innerHTML = maxwords;
        rem.innerHTML = 0;
        return false;
    }
    return true;
} 

</script>

HTML

   <textarea name="Message 1" onkeypress="
 return check_length(this,
 document.getElementById('count1'),
 document.getElementById('remaining1'));"></textarea>
Word count: <span id="count1">0</span> &nbsp;
Words remaining: <span id="remaining1">250</span>

<textarea name="Message 2" onkeypress="
 return check_length(this,
 document.getElementById('count2'),
 document.getElementById('remaining2'));"></textarea>
Word count: <span id="count2">0</span> &nbsp;
Words remaining: <span id="remaining2">500</span>

有没有人知道解决这个问题的方法?

Does anyone know a solution to this problem?

提前感谢,
Tom

Thanks in advance, Tom

推荐答案

向函数中添加一个额外的参数,并发送maxWords从每个函数调用:

Add an extra parameter to your function, and send it the maxWords from each function call:

function check_length(obj, cnt, rem, maxwords)
{
//... rest of the function would stay the same

<textarea name="Message 2" onkeypress="
 return check_length(this,
 document.getElementById('count2'),
 document.getElementById('remaining2'), 250);"></textarea>
Word count: <span id="count2">0</span> &nbsp;
Words remaining: <span id="remaining2">500</span>

要删除剩余的字词,

function check_length(obj, cnt, maxwords)
{
    var ary = obj.value.split(" "); // doubled spaces will throw this off
    var len = ary.length;
    cnt.innerHTML = len;

    if (len > maxwords) {
        alert("Message in '" + obj.name + "' limited to " + maxwords + " words.");
        ary = ary.slice(0,maxwords-1);
        obj.value = ary.join(" "); // truncate additional words
        cnt.innerHTML = maxwords;
        return false;
    }
    return true;
} 

在您的HTML中,

<textarea name="Message 1" onkeypress="
return check_length(this,
document.getElementById('count1'),250);"></textarea>
Word count: <span id="count1">0</span> &nbsp;

这篇关于对多个文本区域的字限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆