gpt4 book ai didi

javascript - 在计算输入类型数字的金额时出现问题

转载 作者:行者123 更新时间:2023-11-28 15:06:13 25 4
gpt4 key购买 nike

我已经使用类编写了 jquery 代码,问题是即使我只更改了一种产品,每种产品也会增加。我想要做的是,如果用户增加一个字段而不是在其父文本框中计算的金额,但实际上当我增加一种产品时,两种产品的金额都会增加。

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8" />
<title></title>
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
$(document).ready(function() {
$(".quantity").change(function(){
$(".subTotal").val(parseInt($(this).val()) * parseInt($(".price").val()));
});

});

</script>
</head>

<body>
<tr>
<td><input type="text" class="price" value="1000" readonly></td>
<td><input type="number" min="0" class="quantity" value="1"></td>
<td><input type="text" class="subTotal" value="1000"></td>
</tr>
<tr>
<td><input type="text" class="price" value="1000" readonly></td>
<td><input type="number" min="0" class="quantity" value="1"></td>
<td><input type="text" class="subTotal" value="1000"></td>
</tr>

</body>

</html>

最佳答案

$(function() {
$(".quantity").change(function(){
$(this).closest('tr').find('.subTotal').val(parseInt($(this).val()) * parseInt($(this).closest('tr').find('.price').val()));
});
});

/*
BEFORE YOU EDITED THE QUESTION
$(function() {
$(".quantity").change(function(){
$(this).nextAll(".subTotal:first").val(parseInt($(this).val()) * parseInt($(this).nextAll('.price:first').val()));
});
});
*/
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<table>
<tr>
<td><input type="text" class="price" value="1000" readonly></td>
<td><input type="number" min="0" class="quantity" value="1"></td>
<td><input type="text" class="subTotal" value="1000"></td>
</tr>
<tr>
<td><input type="text" class="price" value="1000" readonly></td>
<td><input type="number" min="0" class="quantity" value="1"></td>
<td><input type="text" class="subTotal" value="1000"></td>
</tr>
</table>

<!--
BEFORE YOU EDITED THE QUESTION
<input type="number" min="0" class="quantity" value="1">
<input type="text" class="price" value="1000" readonly>
<input type="text" class="subTotal" value="1000">

<input type="number" min="0" class="quantity" value="1">
<input type="text" class="price" value="1000" readonly>
<input type="text" class="subTotal" value="1000">
-->

关于javascript - 在计算输入类型数字的金额时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38802049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com