[英]Multiply input value by data-value and then sum the results
I have nine inputs text and I need to multiply the input value by data-value of each input and then sum the results and display ir on the div. 我有九个输入文本,我需要将输入值乘以每个输入的数据值,然后将结果求和并在div上显示ir。
Input value-1 x data-value-1 输入值1 x数据值1
+ +
Input value-2 x data-value-2 输入值2 x数据值2
+ +
Input value-3 x data-value-3 输入值3 x数据值3
... until 9 ...直到9
My code and how far I've gotten: 我的代码和我走了多远:
$(function() { $('.insert').on('keydown', '.quantity-input', function(e){-1!==$.inArray(e.keyCode,[46,8,9,27,13,110,190])||/65|67|86|88/.test(e.keyCode)&&(!0===e.ctrlKey||!0===e.metaKey)||35<=e.keyCode&&40>=e.keyCode||(e.shiftKey||48>e.keyCode||57<e.keyCode)&&(96>e.keyCode||105<e.keyCode)&&e.preventDefault()}); }) $(".quantity-input").change(function(){ if($(this).val()=="") $(this).val(0); var total = 0; $(".quantity-input").each(function(index,element){ if ($(element).val()) { total+= parseInt($(element).val()); } }); $(".subtotal strong").text(total); }); $('.quantity-input').keyup(function(event) { var b = $(this).val(); var c = $(this).data('value'); var d = b * c alert(d) });
table { width: 100%; } table th { text-align: center; } table td { padding: 10px; width: 10%; } table td input { width: 100%; text-align: center; }
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <thead> <tr> <th>1</th> <th>2</th> <th>3</th> <th>4</th> <th>5</th> <th>6</th> <th>7</th> <th>8</th> <th>9</th> </tr> </thead> <tbody> <tr> <td class="insert"><input type="text" class="quantity-input" data-value="5" value="0"></td> <td class="insert"><input type="text" class="quantity-input" data-value="5" value="0"></td> <td class="insert"><input type="text" class="quantity-input" data-value="5" value="0"></td> <td class="insert"><input type="text" class="quantity-input" data-value="5" value="0"></td> <td class="insert"><input type="text" class="quantity-input" data-value="5" value="0"></td> <td class="insert"><input type="text" class="quantity-input" data-value="5" value="0"></td> <td class="insert"><input type="text" class="quantity-input" data-value="5" value="0"></td> <td class="insert"><input type="text" class="quantity-input" data-value="5" value="0"></td> <td class="insert"><input type="text" class="quantity-input" data-value="5" value="0"></td> </tr> <tr> <td class="subtotal" colspan="9">subtotal: <strong>00</strong></td> </tr> <tr> <td class="total" colspan="9">Total: <strong>00</strong></td> </tr> </tbody> </table>
The idea is to on change of an input, you update the subtotal and total. 想法是在更改输入时,更新小计和总计。
inside the $(".quantity-input").each
subtotal will be the sum of all input, and total will be all input with correspond data-value. 在$(".quantity-input").each
小计将是所有输入的总和,而total将是所有具有相应数据值的输入。
$(function() { $('.insert').on('keydown', '.quantity-input', function(e) { -1 !== $.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) || /65|67|86|88/.test(e.keyCode) && (!0 === e.ctrlKey || !0 === e.metaKey) || 35 <= e.keyCode && 40 >= e.keyCode || (e.shiftKey || 48 > e.keyCode || 57 < e.keyCode) && (96 > e.keyCode || 105 < e.keyCode) && e.preventDefault() }); }) $('.quantity-input').on('change', function(event) { if ($(this).val() == "") $(this).val(0); var subtotal = 0; var total = 0; $(".quantity-input").each(function(index, element) { if ($(element).val()) { subtotal += parseInt($(element).val()); total += parseInt($(element).val() * $(this).data('value')); } }); $(".subtotal > strong").text(subtotal); $(".total > strong").text(total); });
table { width: 100%; } table th { text-align: center; } table td { padding: 10px; width: 10%; } table td input { width: 100%; text-align: center; }
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <thead> <tr> <th>1</th> <th>2</th> <th>3</th> <th>4</th> <th>5</th> <th>6</th> <th>7</th> <th>8</th> <th>9</th> </tr> </thead> <tbody> <tr> <td class="insert"><input type="text" class="quantity-input" data-value="5" value="0"></td> <td class="insert"><input type="text" class="quantity-input" data-value="5" value="0"></td> <td class="insert"><input type="text" class="quantity-input" data-value="5" value="0"></td> <td class="insert"><input type="text" class="quantity-input" data-value="5" value="0"></td> <td class="insert"><input type="text" class="quantity-input" data-value="5" value="0"></td> <td class="insert"><input type="text" class="quantity-input" data-value="5" value="0"></td> <td class="insert"><input type="text" class="quantity-input" data-value="5" value="0"></td> <td class="insert"><input type="text" class="quantity-input" data-value="5" value="0"></td> <td class="insert"><input type="text" class="quantity-input" data-value="5" value="0"></td> </tr> <tr> <td class="subtotal" colspan="9">subtotal: <strong>00</strong></td> </tr> <tr> <td class="total" colspan="9">Total: <strong>00</strong></td> </tr> </tbody> </table>
To make it simple I used input
event for the input 为了简单起见,我使用input
事件作为输入
$(document).ready(function() { $(".quantity-input").on('input',function(){ var subtotal = 0; var total = 0; $(".quantity-input").each(function(index,element){ if ($(element).val()) { subtotal += parseInt($(element).val()); total += parseInt($(element).val()) * parseInt($(element).data('value')); } }); $(".subtotal strong").text(subtotal); $(".total strong").text(total); }); });
table { width: 100%; } table th { text-align: center; } table td { padding: 10px; width: 10%; } table td input { width: 100%; text-align: center; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <thead> <tr> <th>1</th> <th>2</th> <th>3</th> <th>4</th> <th>5</th> <th>6</th> <th>7</th> <th>8</th> <th>9</th> </tr> </thead> <tbody> <tr> <td class="insert"><input type="text" class="quantity-input" data-value="5" value="0"></td> <td class="insert"><input type="text" class="quantity-input" data-value="5" value="0"></td> <td class="insert"><input type="text" class="quantity-input" data-value="5" value="0"></td> <td class="insert"><input type="text" class="quantity-input" data-value="5" value="0"></td> <td class="insert"><input type="text" class="quantity-input" data-value="5" value="0"></td> <td class="insert"><input type="text" class="quantity-input" data-value="5" value="0"></td> <td class="insert"><input type="text" class="quantity-input" data-value="5" value="0"></td> <td class="insert"><input type="text" class="quantity-input" data-value="5" value="0"></td> <td class="insert"><input type="text" class="quantity-input" data-value="5" value="0"></td> </tr> <tr> <td class="subtotal" colspan="9">subtotal: <strong>00</strong></td> </tr> <tr> <td class="total" colspan="9">Total: <strong>00</strong></td> </tr> </tbody> </table>
Additional Information you can combine event functions
input
,change
... etc 附加信息,您可以组合事件函数的input
,change
等like
$(".quantity-input").on('input change',function(){
像$(".quantity-input").on('input change',function(){
I hope this is what you are trying to achieve. 我希望这是您要实现的目标。
$(function() { $('.insert').on('keydown', '.quantity-input', function(e) { -1 !== $.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) || /65|67|86|88/.test(e.keyCode) && (!0 === e.ctrlKey || !0 === e.metaKey) || 35 <= e.keyCode && 40 >= e.keyCode || (e.shiftKey || 48 > e.keyCode || 57 < e.keyCode) && (96 > e.keyCode || 105 < e.keyCode) && e.preventDefault() }); }) $(".quantity-input").on('keyup', function() { if ($(this).val() == "") $(this).val(0); var subtotal = 0; var total = 0; // accounts for data attr values $(".quantity-input").each(function(index, element) { element = $(element); if (element.val()) { subtotal += parseInt(element.val()); total += parseInt(element.val()) * element.data('value'); } }); $(".subtotal strong").text(subtotal); $(".total strong").text(total); });
table { width: 100%; } table th { text-align: center; } table td { padding: 10px; width: 10%; } table td input { width: 100%; text-align: center; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <thead> <tr> <th>1</th> <th>2</th> <th>3</th> <th>4</th> <th>5</th> <th>6</th> <th>7</th> <th>8</th> <th>9</th> </tr> </thead> <tbody> <tr> <td class="insert"> <input type="text" class="quantity-input" data-value="5" value="0"> </td> <td class="insert"> <input type="text" class="quantity-input" data-value="5" value="0"> </td> <td class="insert"> <input type="text" class="quantity-input" data-value="5" value="0"> </td> <td class="insert"> <input type="text" class="quantity-input" data-value="5" value="0"> </td> <td class="insert"> <input type="text" class="quantity-input" data-value="5" value="0"> </td> <td class="insert"> <input type="text" class="quantity-input" data-value="5" value="0"> </td> <td class="insert"> <input type="text" class="quantity-input" data-value="5" value="0"> </td> <td class="insert"> <input type="text" class="quantity-input" data-value="5" value="0"> </td> <td class="insert"> <input type="text" class="quantity-input" data-value="5" value="0"> </td> </tr> <tr> <td class="subtotal" colspan="9">subtotal: <strong>00</strong></td> </tr> <tr> <td class="total" colspan="9">Total: <strong>00</strong></td> </tr> </tbody> </table>
I tried reading your code... But I found it quite confusing and awkward. 我尝试阅读您的代码...但是我发现它相当混乱和尴尬。 This code should produce the described outcome. 该代码应产生描述的结果。 Hope it helps :) 希望能帮助到你 :)
var a = 0,
b = 0,
c = 0,
d = 0;
$(".quantity-input").keyup(function(){
$(".quantity-input").each(function(){
// a holds value of input
a = parseInt($(this).val());
// b holds multiple of value and data-value
b = a * $(this).data("value");
console.log(b);
// c holds subtotal
c += a;
// d holds total
d += b;
});
$(".subtotal strong").html(c);
$(".total strong").html(d);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.