2011-11-18 4 views

Répondre

1

Comme une réponse alternative démontre un peu plus de concepts avancés jQuery pour vous, considérez le javascript suivant:

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

}); 

Est-ce que cela a du sens? Voir here pour un exemple de travail jsFiddle.

+0

J'ai beaucoup aimé ... beaucoup – sebas

1

Je pense que vous devriez chercher quelque chose comme ça!

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

esprit que j'ai ajouté un identifiant à la case à cocher, vous pouvez wanna changer le sélecteur de travailler avec la propriété nom au lieu

+0

c'est génial! mais une autre sous-question. Comment ajouteriez-vous également la valeur finale à la valeur d'une entrée? Disons que le TOTAL est de 400. Je dois mettre 400 dans une valeur d'entrée = "ici" parce que c'est le prix qui est posté à un service de caisse – sebas

+0

http://jsfiddle.net/9YGBu/15/ cela aide-t-il ?? ? – renatoargh

+0

définitivement OUI, merci! – sebas

0

Peut-être quelque chose comme ça, je en cache le sélecteur d'optimisation :

$(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)); 
     } 
    }); 
}); 
Questions connexes