简体   繁体   English

如果文本是在 textarea 中生成的,Javascript 字数统计功能不起作用

[英]Javascript word count function not working if the text are generated in the textarea

How to realtime reflect the word count of textarea.如何实时反映textarea的字数。 The below script works great for count characters (even non-English languages are OK).下面的脚本适用于计数字符(即使是非英语语言也可以)。 But the problem is that it won't work if the text is generated from the script itself.但问题是,如果文本是从脚本本身生成的,它将无法工作。 I had changed the event from "keyup" to "change" to "input", but none of them will work.我已将事件从“keyup”更改为“change”到“input”,但它们都不起作用。 they only work when typing in the textarea.它们仅在输入文本区域时起作用。

Anyone gives me some ideas?有人给我一些想法吗?

 var outputCombo = document.getElementById("outputCombo"); var textInfo = document.getElementById("text-info "); if (document.querySelector('input[name="radio_btn"]')) { document.querySelectorAll('input[name="radio_btn"]').forEach((elem, i) => { elem.addEventListener("change", function(event) { outputCombo.value = "you pressed " + elem.value; }); }); } outputCombo.addEventListener("input", function() { var maxLength = 280; var strLength = outputCombo.value.replace(/[^\\x00-\\xff]/g, "01 ").length; var charRemain = (maxLength - strLength); if (charRemain < 0) { textInfo.innerHTML = '<span style="color: red; ">surpassed ' + charRemain + ' chracters</span>'; outputCombo.style.backgroundColor = 'red'; } else { textInfo.innerHTML = 'remaining: ' + charRemain; outputCombo.style.backgroundColor = 'white'; } });
 <ul class="ul"> <li class=""> <input type="radio" name="radio_btn" value="Instagram"> <label>Instagram</label> </li> <li class=""> <input type="radio" name="radio_btn" value="Twitter"><label>Twitter</label> </li> </ul> <textarea class="form-control" id="outputCombo"></textarea> <span id="text-info ">word count: </span>

I add your script for count into a function then use it when you change value of textarea like:我将您的计数脚本添加到一个函数中,然后在您更改 textarea 的值时使用它,例如:

 var outputCombo = document.getElementById("outputCombo"); var textInfo = document.getElementById("text-info "); if (document.querySelector('input[name="radio_btn"]')) { document.querySelectorAll('input[name="radio_btn"]').forEach((elem, i) => { elem.addEventListener("change", function(event) { outputCombo.value = "you pressed " + elem.value; countTextArea(); }); }); } outputCombo.addEventListener("input", function() { countTextArea(); }); function countTextArea() { var maxLength = 280; var strLength = outputCombo.value.replace(/[^\\x00-\\xff]/g, "01 ").length; var charRemain = (maxLength - strLength); if (charRemain < 0) { textInfo.innerHTML = '<span style="color: red; ">surpassed ' + charRemain + ' chracters</span>'; outputCombo.style.backgroundColor = 'red'; } else { textInfo.innerHTML = 'remaining: ' + charRemain; outputCombo.style.backgroundColor = 'white'; } }
 <ul class="ul"> <li class=""> <input type="radio" name="radio_btn" value="Instagram"> <label>Instagram</label> </li> <li class=""> <input type="radio" name="radio_btn" value="Twitter"><label>Twitter</label> </li> </ul> <textarea class="form-control" id="outputCombo"></textarea> <span id="text-info ">word count: </span>

You can manually trigger an input event with outputCombo.dispatchEvent(new Event("input"))您可以使用outputCombo.dispatchEvent(new Event("input"))手动触发输入事件

Here's the full code:这是完整的代码:

<ul class="ul">
    <li class="">
        <input type="radio" name="radio_btn" value="Instagram">
        <label>Instagram</label>
    </li>
    <li class="">
        <input type="radio" name="radio_btn" value="Twitter"><label>Twitter</label>
    </li>
</ul>
<textarea class="form-control" id="outputCombo"></textarea>
<span id="text-info ">word count: </span>

<script type="text/javascript ">
    var outputCombo = document.getElementById("outputCombo");
    var textInfo = document.getElementById("text-info ");

    if (document.querySelector('input[name="radio_btn"]')) {
        document.querySelectorAll('input[name="radio_btn"]').forEach((elem, i) => {
            elem.addEventListener("change", function(event) {
                outputCombo.value = "you pressed " + elem.value;

                // Here is the triggered event
                outputCombo.dispatchEvent(new Event("input"))

            });
        });
    }



    outputCombo.addEventListener("input", function() {
        var maxLength = 280;
        var strLength = outputCombo.value.replace(/[^\x00-\xff]/g, "01 ").length;
        var charRemain = (maxLength - strLength);
        if (charRemain < 0) {
            textInfo.innerHTML = '<span style="color: red; ">surpassed ' + charRemain + ' chracters</span>';
            outputCombo.style.backgroundColor = 'red';
        } else {
            textInfo.innerHTML = 'remaining: ' + charRemain;
            outputCombo.style.backgroundColor = 'white';
        }
    });
</script>

Fire the event manually once you set the value to textarea将值设置为textarea手动textarea

outputCombo.value = "you pressed " + elem.value;
outputCombo.dispatchEvent(new Event('input'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM