2011-11-18 53 views

回答

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); 

}); 

这有道理吗?请参阅here以了解正在运行的jsFiddle示例。

+0

我喜欢这个...很多 – sebas

1

我想你应该寻找这样的事情!我已经添加了ID的复选框

http://jsfiddle.net/9YGBu/21/

介意,你可能会想改变选择与name属性的工作,而不是

+0

的伟大!但还有一个子问题。你还将如何将最终值添加到输入值? 比方说一共是400,我需要把400在输入的值=“这里”监守这就是那个被发布到结账服务 – sebas

+0

http://jsfiddle.net/9YGBu/15/它有助于价格? ? – renatoargh

+0

肯定是的,谢谢! – sebas

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)); 
     } 
    }); 
});