Javascript 如何在不点击输入字段的情况下更新输入值?
所以我有一个问题,我的输入值只有在我点击输入字段(“总计”输入字段,在“Suma”(“Kiekis”*“Kaina”=“Suma”)列下面,并且是只读的时候才会更新,但我想自动更新“总计”字段(在底部靠近“Bendra Suma”) JS代码:Javascript 如何在不点击输入字段的情况下更新输入值?,javascript,jquery,html,Javascript,Jquery,Html,所以我有一个问题,我的输入值只有在我点击输入字段(“总计”输入字段,在“Suma”(“Kiekis”*“Kaina”=“Suma”)列下面,并且是只读的时候才会更新,但我想自动更新“总计”字段(在底部靠近“Bendra Suma”) JS代码: $(document).on('keyup change', '.quantity, .price', function() { var row = $(this).closest('tr').get(0); var rowPrice =
$(document).on('keyup change', '.quantity, .price', function() {
var row = $(this).closest('tr').get(0);
var rowPrice = $(row).find('.price').val();
var rowQuantity = $(row).find('.quantity').val();
$(row).find('.total').val((rowPrice * rowQuantity).toFixed(2));
$('.total').blur(function () {
var sum = 0;
$('.total').each(function() {
sum += parseFloat($(this).val());
});
$('#totals').val((sum).toFixed(2));
});
});
您可以测试它在JSFIDLE中的工作方式:
提前感谢您的帮助 删除更改,以便在按键时激活
$(document).on('keyup', '.quantity, .price', function() {
删除更改,以便在键控时激活
$(document).on('keyup', '.quantity, .price', function() {
您不需要将代码放入模糊事件处理程序中 把它放在keyup/change事件处理程序部分 删除第8行
$(document).on('keyup', '.quantity, .price', function() {
最后一行
});
所以你有这样的东西
$(document).on('keyup change', '.quantity, .price', function() {
var row = $(this).closest('tr').get(0);
var rowPrice = $(row).find('.price').val();
var rowQuantity = $(row).find('.quantity').val();
$(row).find('.total').val((rowPrice * rowQuantity).toFixed(2));
var sum = 0;
$('.total').each(function() {
sum += parseFloat($(this).val()) || 0; // If a string is entered, use 0 instead.
});
$('#totals').val((sum).toFixed(2));
});
您不需要将代码放入模糊事件处理程序中 把它放在keyup/change事件处理程序部分 删除第8行
$(document).on('keyup', '.quantity, .price', function() {
最后一行
});
所以你有这样的东西
$(document).on('keyup change', '.quantity, .price', function() {
var row = $(this).closest('tr').get(0);
var rowPrice = $(row).find('.price').val();
var rowQuantity = $(row).find('.quantity').val();
$(row).find('.total').val((rowPrice * rowQuantity).toFixed(2));
var sum = 0;
$('.total').each(function() {
sum += parseFloat($(this).val()) || 0; // If a string is entered, use 0 instead.
});
$('#totals').val((sum).toFixed(2));
});
这将更新总数
$(document).on('keyup change', '.quantity, .price', function() {
var row = $(this).closest('tr').get(0);
var rowPrice = $(row).find('.price').val();
var rowQuantity = $(row).find('.quantity').val();
$(row).find('.total').val((rowPrice * rowQuantity).toFixed(2));
// Get all the totals from each row and convert them to
// an array of floats. Then sum them together with reduce
var totalPrice = $('.total').map(function (index, el) {
return parseFloat($(el).val()) || 0;
}).get().reduce(function (a,b) { return a + b }, 0)
// Update the total field with the calculated totals
$('#totals').val(totalPrice)
});
这将更新总数
$(document).on('keyup change', '.quantity, .price', function() {
var row = $(this).closest('tr').get(0);
var rowPrice = $(row).find('.price').val();
var rowQuantity = $(row).find('.quantity').val();
$(row).find('.total').val((rowPrice * rowQuantity).toFixed(2));
// Get all the totals from each row and convert them to
// an array of floats. Then sum them together with reduce
var totalPrice = $('.total').map(function (index, el) {
return parseFloat($(el).val()) || 0;
}).get().reduce(function (a,b) { return a + b }, 0)
// Update the total field with the calculated totals
$('#totals').val(totalPrice)
});
已尝试。没有帮助,我只是更改了Kaina字段,总和会自动更新。请输入您的答案。。。但它缺少关闭此语句的代码。已尝试。没有帮助,我只是更改了Kaina字段,总和会自动更新。请输入您的答案。。。但是它缺少结束此语句的代码。感谢编辑我的答案wot,对您的答案进行了投票并接受了您的编辑:)感谢编辑我的答案wot,对您的答案进行投票并接受了您的编辑:)