Javascript在表单输入框中自动计数字符并在表单数量输入框中输入结果
Javascript to automatically Count Characters in Form Input Box and Enter result in Form Quantity Input Box?
我有一个工作的javascript,将自动计数字符的TextArea和输入结果在另一个文本区域。
然而,我正试图修改此工作与现有的购物车表单,在这种形式下,它必须在一个失败。
这个表单有一个区域供客户输入文本,脚本将对字符进行计数,结果数字是客户购买的商品数量。
下面是我的代码在哪里工作的例子。包括我的购物车表单的缩短示例。
我怎样才能使它与表单一起工作?
这是我的javascript:
<script type="text/javascript">
function countChars(countfrom,displayto) {
var len = document.getElementById(countfrom).value.length;
document.getElementById(displayto).innerHTML = len;
}
</script>
这是我的HTML:
<body>
<!-- This example updates correctly to a <textarea> HOWEVER, it needs to work with a Form <input type="text";> as shown below -->
<textarea id="enter-your-text-input" onkeyup="countChars('enter-your-text-input','character-count-input');" name="product1[]" maxlength="200" value="" style="position:absolute; left:27px; top:28px; width:320px; color:#4B4B4B; font-size:18.0px; height:26px; width:320px;/*Tag Style*/"></textarea>
<textarea id="character-count-input" name="qty1" value="characters" style="position:absolute; left:247px; top:72px; width:100px; color:#4B4B4B; font-size:18.0px; height:26px; width:100px;/*Tag Style*/"></textarea>
<!-- I NEED to Count the Characters and display the results in this form-->
<form id="shopping-cart-form" onsubmit="return validate_shopping-cart-form(this)" action="http://www.someaddress.com" method="post" target="cart" style="margin:0;position:absolute;left:0px;top:120px;width:372px;height:546px;">
<input type="text" id="enter-your-text-input" onkeyup="countChars('enter-your-text-input','character-count-input');" name="product1[]" maxlength="200" value="" style="position:absolute; left:27px; top:28px; width:320px; color:#4B4B4B; font-size:18.0px; height:26px; width:320px;/*Tag Style*/"></textarea>
<input type="text" id="character-count-input" name="qty1" value="characters" style="position:absolute; left:247px; top:72px; width:100px; color:#4B4B4B; font-size:18.0px; height:26px; width:100px;/*Tag Style*/"></textarea>
</form>
</body>
对于<input>
元素,您需要通过.value
分配值,以下是正确的代码:
function countChars(countfrom,displayto) {
var len = document.getElementById(countfrom).value.length;
document.getElementById(displayto).value = len;
}
相关文章:
- Nodejs服务器在解析之前生成输入表单
- 如何创建打开警报框的输入表单
- JavaScript onChange 事件侦听器对输入表单不起作用
- 如何在 OnKeyPress 事件后获取输入表单元素的值
- 单输入表单 - 特殊情况 |停止表单提交
- 需要输入表单验证
- 如何在谷歌图表中使用输入表单值
- 使用jquery可交换地更改两个html输入表单值
- 当从jqueryajax调用输入表单时,它不起作用
- 控制器作为搜索输入表单的语法
- 删除带有按钮的输入表单失败
- 验证带有2个小数点的浮点形式的输入表单
- AngularJS:输入表单在分部中不起作用
- 如何从php中的输入表单调用Javascript函数,以更改HTML
- 输入表单中的Sharepoint javascript
- 当点击输入表单时,为Raphael制作的对象添加光晕
- 当用户将数据输入表单时要知道的事件
- 保存输入表单后ImageMap颜色更改
- 链接输入表单中的填充脚本
- 有关多个输入表单验证的问题