【发布时间】:2015-08-24 22:54:23
【问题描述】:
我有一个包含 3 个文本区域、一个复制按钮和一个重置按钮的表单。我想将所有字符加到一个总和中,然后在复制/重置按钮旁边显示该总和。有 500 个字符的限制,计数器应从 49 个字符开始。我是否应该将所有文本区域并将它们“漏斗”到一个var中,然后计算那个var?我不确定我应该如何处理这个问题。我试过this technique
但它只适用于一个文本区域,而不是所有文本区域的总和。如果字符数超过 500,我希望文本变为红色并说“您已超出字符数限制”。一旦超过 500,我不想限制或限制文本。我有点担心试图找到解决方案,而且我是一个明显的 html/javascript 新手。
我不需要担心 Firefox/opera 中的回车问题,因为每个人都会使用 IE11。
<h1>
Enter your notes into the text boxes below
</h1>
<p>
Please avoid using too many abbreviations so others can read your notes.
</p>
<form>
<script type="text/javascript"><!--
// input field descriptions
var desc = new Array();
desc['kcall'] = 'Reason for Call';
desc['pact'] = 'Actions Taken';
desc['mrec'] = 'Recommendations';
function CopyFields(){
var copytext = '';
for(var i = 0; i < arguments.length; i++){
copytext += desc[arguments[i]] + ': ' + document.getElementById (arguments[i]).value + '\n';
}
var tempstore = document.getElementById(arguments[0]).value;
document.getElementById(arguments[0]).value = copytext;
document.getElementById(arguments[0]).focus();
document.getElementById(arguments[0]).select();
document.execCommand('Copy');
document.getElementById(arguments[0]).value = tempstore;
document.getElementById("copytext").reset();
}
--></script>
<p> Reason For Call: </p> <textarea rows="5" cols="40" id="kcall"></textarea><br>
<p> Actions Taken: </p> <textarea rows="5" cols="40" id="pact"></textarea><br>
<p> Recommendations: </p> <textarea rows="5" cols="40" id="mrec"></textarea><br>
<br>
<button type="button" onclick="CopyFields('kcall', 'pact', 'mrec');">Copy Notes</button>
<input type="reset" value="Reset"/>
</form>
【问题讨论】:
标签: javascript html forms counter