我更新了事件的输入值,并调用了一个函数来计算它。但功能无法读取输入值。在下面的例子中,当我按下价格按钮时,我必须计算总数。 http://jsfiddle.net/LkaA7/4/
// on change of quantity - works
$('.qty').on('change', function(){
calculate();
});
// on change of price - works
$('.price').on('change', function(){
calculate();
});
// on clicking button - not working
$('p.button').on('click', function(){
var price = $(this).data('price');
/*** in my project this val is generated by ajax call ***/
$('.price').val(price);
/***** ******/
calulate();
});
function calculate(){
var qty = $('.qty'),
pr = $('.price'),
tot = $('.total'),
cal = 0;
cal = qty.val()*pr.val();
tot.val(cal);
return;
}
答案 0 :(得分:0)
为了好玩,我采取了略微不同的方法。 FIDDLE
价格变动转为.blur。 检查了价格中数字输入的错误。
JS
$('.qty').on('change', function(){
calculate();
});
$('.price').blur(function(){
calculate();
});
$('.button').on('click', function(){
calculate();
});
function calculate(){
var pr = $('.price').val();
if( isNaN(+pr) )
{
pr = 0;
}
var qty = $('.qty').val();
tot = qty * pr;
$('.total').val(tot);
}