当你检查那个方框时如何加25,当你在span id =“preciototal”内的数字取消选中时减去25?
请看看这里。 它是结帐表单的一部分,其想法是计算总价格:
答案 0 :(得分:1)
答案 1 :(得分:1)
作为向您展示一些更高级的jQuery概念的替代答案,请考虑以下javascript:
$(document).ready(function() {
// cache anything reused in variables
var $zonaenvio = $("#zonaenvio");
var $gelypilas = $('#gelypilas');
var $totalprecio = $('#totalprecio');
var $totalSpan = $('#preciototal');
// declare a function that does everything
var updatePrice = function() {
// get the value of the selected product, or 0 if nothing selected
var productAmount = parseInt($zonaenvio.val() || '0');
if (productAmount > 0) {
// if something selected, calculate correct total and show it
var correctTotal = productAmount + ($gelypilas.is(':checked') ? 25 : 0);
$totalSpan.text(correctTotal);
$totalprecio.show('slow');
} else {
// otherwise, if nothing selected, hide the total information
$totalprecio.hide();
}
};
// bind the function to the input events
$zonaenvio.change(updatePrice).change(); // trigger once if needed
$gelypilas.change(updatePrice);
});
这有意义吗?有关正常工作的jsFiddle示例,请参阅here。
答案 2 :(得分:0)
也许是这样的,我缓存了选择器进行优化:
$(document).ready(function() {
var precioTotal = $("#preciototal"), gelypilas = $('#gelypilas'),
totalPrecio = $('#totalprecio'), zonaEnvio = $("#zonaenvio");
zonaEnvio.change(function() {
precioTotal.text($(this).val());
gelypilas.removeAttr('checked');
totalPrecio.show("slow");
}).change(); // trigger once if needed
gelypilas.change(function(){
if(gelypilas.attr('checked')){
precioTotal.text(parseInt(precioTotal.text()) + 25);
}else{
precioTotal.text(Math.max(0, parseInt(precioTotal.text()) - 25));
}
});
});