- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试使用 jquery 计算表上的总计,但是运行时总计不会出现。所以我想在输入输入值时显示总直播,总自动求和,这是哪里错了?
HTML
<table class="table table-condensed">
<thead>
<tr>
<td class="text-left"><strong>Product</strong></td>
<td class="text-center"><strong>Value</strong></td>
</tr>
</thead>
<tbody>
<tr>
<td>Microwave</td>
<td><input type="text" id="product_a" name="producta" value="12.000"></td>
</tr>
<tr>
<td>Microwave</td>
<td><input type="text" id="product_b" name="producta" value="19.000"></td>
</tr>
<tr>
<td>Microwave</td>
<td><input type="text" id="product_c" name="producta" value="15.000"></td>
</tr>
<tr>
<td class="thick-line"><strong>Total</strong></td>
<td class="thick-line"><input type="text" id="total" name="total" /></td>
</tr>
</tbody>
</table>
JS
jQuery(document).ready(function(){
var total = ($('#product_a').val() + $('#product_b').val() + $('#product_c').val()) ;
$('#total').val(total);
});
最佳答案
你做错了。 val()
在 input[type=text]
上返回一个字符串,目前您只是连接字符串值。
I want to show the total live when I enter the input value
然后,当您键入时输入发生更改时,您需要添加一个监听器。这可以通过 keyup
事件来完成。
例如
$(function() {
var $aProduct = $('#product_a'),
$bProduct = $('#product_b'),
$cProduct = $('#product_c'),
$total = $('#total'),
runTotal = function() {
var a = parseInt($aProduct.val()),
b = parseInt($bProduct.val()),
c = parseInt($cProduct.val());
$total.val(a + b + c);
};
$('.inputVal').on('keyup', runTotal);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-condensed">
<thead>
<tr>
<td class="text-left"><strong>Product</strong>
</td>
<td class="text-center"><strong>Value</strong>
</td>
</tr>
</thead>
<tbody>
<tr>
<td>Microwave</td>
<td>
<input type="text" id="product_a" name="producta" class="inputVal" value="12.000">
</td>
</tr>
<tr>
<td>Microwave</td>
<td>
<input type="text" id="product_b" name="productb" class="inputVal" value="19.000">
</td>
</tr>
<tr>
<td>Microwave</td>
<td>
<input type="text" id="product_c" name="productc" class="inputVal" value="15.000">
</td>
</tr>
<tr>
<td class="thick-line"><strong>Total</strong>
</td>
<td class="thick-line">
<input type="text" id="total" name="total" />
</td>
</tr>
</tbody>
</table>
关于javascript - 表上 jquery 的总值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41113670/
创建我用下面的代码制作了分布图: from numpy import * import numpy as np import matplotlib.pyplot as plt sigma = 4.1
我想我已经尝试了这方面的所有方法,但我无法弄清楚这一点。我只是想在用户增加数量时更新表中每一行的总价。到目前为止我已经尝试过 HTML 中的数学:data-bind="text: parseFloat
我的数据库中有一个表,如下所示: id | date | amount ======================== 1 | 2015-01-26 | 1000 2 | 2015-
我有一个条形图,总值通常重叠在上面,如下所示。我尝试减少图形的面积、宽度和高度,这个问题得到解决,但对于其他一些值再次出现,是否有任何永久的解决方案来避免重叠? 请查找下面的代码 $('#' + d
我一直在四处寻找,似乎找不到这个问题的答案,希望得到任何帮助。 简而言之,我有两个表:付款表和成员(member)表。 我已执行以下操作来查找成员(member) ID 与付款 ID 列中的 ID 匹
我是一名优秀的程序员,十分优秀!