dpowt82802 2015-08-03 07:06
浏览 22
已采纳

使用php和Javascript的多个文本区域的最大长度

I am trying to display the number of characters left in multiple text areas. Although I have different ID's for the text areas, the max length is prompted only for the 2nd text area and not for the first. My code is as shown below

<textarea id="txtBox"></textarea> 
    <input type="text" id="counterBox"/>
        <script>
            var txtBoxRef = document.querySelector("#txtBox");
            var counterRef = document.querySelector("#counterBox");
            txtBoxRef.addEventListener("keydown",function(){
            var remLength = 0;
            remLength = 160 - parseInt(txtBoxRef.value.length);
            if(remLength < 0)
            {
                txtBoxRef.value = txtBoxRef.value.substring(0, 160);
                return false;
            }
            counterRef.value = remLength + " characters remaining...";
            },true);
        </script>

<textarea id="txtBox1"></textarea> 
    <input type="text" id="counterBox1"/>
            <script>
                var txtBoxRef = document.querySelector("#txtBox1");
                var counterRef = document.querySelector("#counterBox1");
                txtBoxRef.addEventListener("keydown",function(){
                var remLength = 0;
                remLength = 160 - parseInt(txtBoxRef.value.length);
                if(remLength < 0)
                {
                txtBoxRef.value = txtBoxRef.value.substring(0, 160);
                return false;
                }
                counterRef.value = remLength + " characters remaining...";
                },true);
            </script>

The result is as below (What I have):

enter image description here

What I want :

enter image description here

  • 写回答

2条回答 默认 最新

  • douhui8163 2015-08-03 07:18
    关注

    Looks like the variables txtBoxRef and counterRef are in the same scope area, you declared twice.

    <textarea id="txtBox"></textarea> 
        <input type="text" id="counterBox"/>
            <script>
                var txtBoxRef = document.querySelector("#txtBox");
                var counterRef = document.querySelector("#counterBox");
                txtBoxRef.addEventListener("keydown",function(){
                var remLength = 0;
                remLength = 160 - parseInt(txtBoxRef.value.length);
                if(remLength < 0)
                {
                    txtBoxRef.value = txtBoxRef.value.substring(0, 160);
                    return false;
                }
                counterRef.value = remLength + " characters remaining...";
                },true);
            </script>
    
    <textarea id="txtBox1"></textarea> 
        <input type="text" id="counterBox1"/>
                <script>
                    var txtBoxRef1 = document.querySelector("#txtBox1");
                    var counterRef1 = document.querySelector("#counterBox1");
                    txtBoxRef1.addEventListener("keydown",function(){
                    var remLength = 0;
                    remLength = 160 - parseInt(txtBoxRef1.value.length);
                    if(remLength < 0)
                    {
                    txtBoxRef1.value = txtBoxRef1.value.substring(0, 160);
                    return false;
                    }
                    counterRef1.value = remLength + " characters remaining...";
                    },true);
                </script>

    check Fiddle

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用