Prix modulable avec boutons radio
Bonjour à tous,
J'ai besoin de vos compétences en javascript, je ne suis pas suffisement calé pour m'en sortir tout seul...
Je vous expose mon problème: j'ai récupéré le fichier ci-dessous qui permet d'actualiser le prix total d'un produit en tenant compte des otpions que l'on peux choisir via deux menu déroulants. Ce que je souhaite, c'est pouvoir modifier le prix total également par l'intermédiaire de boutons radio, j'ai donc intégré 3 boutons radio pour faire des essais. Après de nombreuses tentatives, je n'ai pas réussi à modifier le code javascript pour que celà fonctionne également avec ces boutons radio.
Je vous remercie d'avance pour toute l'aide que vous pourrez m'apporter,
Cordialement,
Zardhoz
EDIT: c'est bon j'ai trouvé, je ne suis finalement pas si nul que ça :yaisse2: ci-dessous le code pour ceux que ça intéresse.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| <head>
<title> Small JavaScript Example </title>
<script language="javascript">
<!--
function FormatNumber(num)
{
if(isNaN(num)) { num = "0"; }
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10) { cents = "0" + cents; }
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
{
num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
}
return (((sign)?'':'-') + num + '.' + cents);
}
function showPrice(form)
{
var myTotalPrice = 0;
var showUP = 0;
var myMathProblem = "";
myItemPrice = parseFloat(form.nuPrice.value);
for (var i = 0; i < form.elements.length; i++)
{
var e = form.elements[i];
if ( e.type == 'select-one' )
{
showUP = 1;
Item = e.selectedIndex;
myPrice = e.options[Item].text;
myDollarSign = myPrice.indexOf("$",0)
if ( myDollarSign != "-1" )
{
myParSign = myPrice.indexOf(")", myDollarSign);
myAttributeString = myPrice.substring(myDollarSign+1, myParSign);
myAttributeString = myAttributeString.replace(/,/,"");
myAttributePrice = parseFloat(myAttributeString);
myMathProblem = myPrice.charAt(myDollarSign - 1);
} else { myAttributePrice = 0; }
if (myMathProblem == "+")
{
myTotalPrice = myTotalPrice + myAttributePrice;
} else {
myTotalPrice = myTotalPrice - myAttributePrice;
}
}
}
for (var i=0; i<form.prix_option.length;i++)
{
if (form.prix_option[i].checked)
{
showUP = 1;
Item = form.prix_option[i].title;
myPrice = Item;
myDollarSign = myPrice.indexOf("$",0)
if ( myDollarSign != "-1" )
{
myParSign = myPrice.indexOf(")", myDollarSign);
myAttributeString = myPrice.substring(myDollarSign+1, myParSign);
myAttributeString = myAttributeString.replace(/,/,"");
myAttributePrice = parseFloat(myAttributeString);
myMathProblem = myPrice.charAt(myDollarSign - 1);
} else { myAttributePrice = 0; }
if (myMathProblem == "+")
{
myTotalPrice = myTotalPrice + myAttributePrice;
} else {
myTotalPrice = myTotalPrice - myAttributePrice;
}
}
}
if ( showUP )
{
myTotalPrice = FormatNumber(myTotalPrice + myItemPrice);
document.getElementById("productNEWprice").innerHTML = "Total Price with Options $" + myTotalPrice;
}
}
//-->
</script>
</head>
<html>
<body onload='showPrice(cart_quantity)'>
<form name="cart_quantity">
<input type="hidden" name="nuPrice" value="999.99">
<INPUT type="radio" name="prix_option" value="1" TITLE="OPTION 1 (+$17.00)" onClick="showPrice(this.form)">OPTION 1 (+$17.00)<br>
<INPUT type="radio" name="prix_option" value="2" TITLE="OPTION 2 (+$10.00)" onClick="showPrice(this.form)">OPTION 2 (+$10.00)<br>
<INPUT type="radio" name="prix_option" value="3" TITLE="OPTION 3 (-$21.00)" onClick="showPrice(this.form)">OPTION 3 (-$21.00)<br>
<br>
<select name="prix_option" onChange="showPrice(this.form);">
<option> Select One </option>
<option value="1">OPTION 1 (+$1315.00)</option>
<option value="2">OPTION 2 (+$20.00) </option>
</select><br><br>
<select name="prix_option" onChange="showPrice(this.form);">
<option> Select One </option>
<option value="1"> OPTION 1 (+$15.00)</option>
<option value="2"> OPTION 2 (+$10.00) </option>
<option value="3"> OPTION 3 (-$21.00) </option>
</select>
<br><br><br><br><br><br><div id="productNEWprice"></div>
</form>
</body>
</html> |