gpt4 book ai didi

javascript - Jquery 计算问题(返回 NaN)

转载 作者:行者123 更新时间:2023-11-28 03:07:25 24 4
gpt4 key购买 nike

我有一个表格,其中根据所选的下拉列表进行某些计算。一切正常,除了当我从下拉价格/单位字段中选择一个值时,必须通过计算返回 NaN 的 (total1/qty) 来填充。请帮忙解决这个问题?我的标记如下:

 <html>

<head>
<title>Dept</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.10.1.js'></script>

<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(document).ready(function(){
$("select").on('change',function () {
var dept_number = $(this).val();
var price = $(this).find(':selected').data('price');

$(this).closest('table').find('.deptip').val(dept_number);
$(this).closest('table').find('.priceip').val(price);
$(this).closest('table').find('.total1').val(price * $(this).closest('table').find('.total').data('value'));
$(this).closest('table').find('.price_unit').val($(this).closest('table').find('.total1').data('value') / $(this).closest('table').find('.qty').data('value'));

});
});
});//]]>




</script>

</head>

<body>
<div class="base"> <!--Keep each group in base container-->
<table border="1"><tr>
<td><select id="category0">
<option value="1" data-price="10">USD</option>
<option value="2" data-price="20">INR</option>
<option value="3" data-price="30">AUD</option>
</select></td>
<td><label>Dept. num:</label>
<input type="text" class="deptip" id="dept-input"/><!--give them a class--></td>
<td><label>Price:</label>
<input type="text" class="priceip" id="price-input"/> <!--a class here too--></td>
<td><label>Total:</label><input data-value="50" type="text" readonly class="total" value="50"/></td>
<td><label>Qty:</label><input type="text" class="qty" value="5"/></td>
<td><label>Total1:</label><input data-value="50" type="text" readonly class="total1"/></td>
<td><label>Price/Unit:</label><input type="text" readonly class="price_unit" /></td>


</tr></table>
</div>

<div class="base"> <!--Keep each group in base container-->
<table border="1"><tr>
<td><select id="category1">
<option value="1" data-price="10">USD</option>
<option value="2" data-price="20">INR</option>
<option value="3" data-price="30">AUD</option>
</select></td>
<td><label>Dept. num:</label>
<input type="text" class="deptip" id="dept-input"/><!--give them a class--></td>
<td><label>Price:</label>
<input type="text" class="priceip" id="price-input"/> <!--a class here too--></td>
<td><label>Total:</label><input data-value="50" type="text" readonly class="total" value="50"/></td>
<td><label>Qty:</label><input type="text" class="qty" value="5" /></td>
<td><label>Total1:</label><input data-value="50" type="text" readonly class="total1"/></td>
<td><label>Price/Unit:</label><input type="text" readonly class="price_unit"/></td>

</tr></table>
</div>

<div class="base"> <!--Keep each group in base container-->
<table border="1"><tr>
<td><select id="category2">
<option value="1" data-price="10">USD</option>
<option value="2" data-price="20">INR</option>
<option value="3" data-price="30">AUD</option>
</select></td>
<td><label>Dept. num:</label>
<input type="text" class="deptip" id="dept-input"/><!--give them a class--></td>
<td><label>Price:</label>
<input type="text" class="priceip" id="price-input"/> <!--a class here too--></td>
<td><label>Total:</label><input data-value="50" type="text" readonly class="total" value="50"/></td>
<td><label>Qty:</label><input type="text" class="qty" value="5" /></td>
<td><label>Total1:</label><input data-value="50" type="text" readonly class="total1"/></td>
<td><label>Price/Unit:</label><input type="text" readonly class="price_unit"/></td>


</tr></table>
</div>
</body>

</html>

最佳答案

您的 .qty 输入有一个 value 而您正在尝试获取它的 data-value

试试这个

$("select").on('change', function () {
var dept_number = $(this).val();
var price = $(this).find(':selected').data('price');
$(this).closest('table').find('.deptip').val(dept_number);
$(this).closest('table').find('.priceip').val(price);
$(this).closest('table').find('.total1').val(price * $(this).closest('table').find('.total').data('value'));
$(this).closest('table').find('.price_unit').val($(this).closest('table').find('.total1').data('value') / $(this).closest('table').find('.qty').val());

});

https://jsfiddle.net/zu6uvzxv/2/

关于javascript - Jquery 计算问题(返回 NaN),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31939175/

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