在按键事件脚本上显示其他文本框的总和
it1352
帮助1人
问题说明
我有三个或更多动态创建并具有ID的文本框。我必须显示文本框中所有值的总和,并按下按键上的总和ID。如果文本框为空,则不应将其计入总和中。如果我们在两个文本框中给出值,它会将两个文本框值的总和显示到我的文本框中,该文本框具有id sum。问题是,这有效,但没有给我的文本框正确的值和总和。
我尝试过:
我的剧本
I have three or more textboxes which are created dynamically and have ids. I have to show the sum of all the values in my textbox with the sum id on key press. If a textbox is empty, it should not be accounted in the sum. If we give the value in two textbox than it will display the sum of two textbox value to my textbox which has id sum. The problem is that this works but doesn't give the correct value to my textbox with the sum id.
What I have tried:
my script
<script>
$(document).ready(function () {
//iterate through each textboxes and add keyup
//handler to trigger sum event
$(".form-control").each(function () {
$(this).keyup(function () {
calculateSum();
});
});
});
function calculateSum() {
debugger;
var sum = 0;
//iterate through each textboxes and add the values
$(".form-control").each(function () {
//add only if the value is number
if (!isNaN(this.value) && this.value.length != 0) {
sum = parseFloat(this.value);
}
});
//.toFixed() method will roundoff the final sum to 2 decimal places
// int sums = sum;
//jQuery.noConflict("Debit").value = sums;
//$("#Debit").val(sum);
//$('#sum').html(sum.toFixed(2));
document.getElementById('sum').value = sum;
}
</script>
the textbox where to display sum
<div class="form-group">
<label class="control-label col-md-3">
Debitt:
</label>
<div class="col-md-4">
<input type="text" name="Debit" id="sum" class="form-control"><br>
</br></div>
<span id="errorName" style="color: Red;"></span>
</div>
正确答案
#1
(document).ready(function(){
// 它通过每个文本框进行操作并添加keyup
// 处理程序以触发sum事件
(document).ready(function () { //iterate through each textboxes and add keyup //handler to trigger sum event
( .form-control)。每个( function(){
(".form-control").each(function () {
( this )。keyup(function(){
calculateSum();
});
});
});
函数calculateSum(){
调试器;
var sum = 0 ;
// 遍历每个文本框并添加值
(this).keyup(function () { calculateSum(); }); }); }); function calculateSum() { debugger; var sum = 0; //iterate through each textboxes and add the values
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcgkhee
系列文章
更多
同类精品
更多
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20