PDA

View Full Version : Get price via quantity field



gwmbox
01-07-2011, 01:17 AM
Hi guys, I am using a script that was provided to me for changing an image based on the selection of several selects (http://www.dynamicdrive.com/forums/showthread.php?t=59873)

However now what I am trying to do is set a value for quantity and have that change the result if the quantity changes. My modified code looks like;



function mpdprice(){
var mpprice = '1';
var string = document.getElementById('quantity-input').value;
if (string == '1'){
mpprice='22.80';
}
else if (string >= '2' && string <= '24' ){
mpprice='22.08';
}
else if (string >= '25' && string <= '49' ){
mpprice='21.36';
}
else if (string >= '50' && string <= '99' ){
mpprice='19.93';
}
else if (string >= '100' && string <= '249' ){
mpprice='18.49';
}
else if (string >= '250'){
mpprice='17.06';
}
document.getElementById('matprice').innerHTML='$'+mpprice+' each';
}


and the HTML for the price value and that field is



<span id="matprice">$22.80 each</span>
<input type="text" maxlength="5" value="1" id="quantity-input" onchange="mpdprice();">


While it appears to work some values are not correct, if you test it you will see for example setting a quantity to 10 gives $1.00????

Please help me fix the code.

Thanks

GW

vwphillips
01-07-2011, 11:16 AM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script type="text/javascript">
/*<![CDATA[*/
function mpdprice(){
var mpprice = '1';
var string = document.getElementById('quantity-input').value.replace(/\D/g,''); // remove all none digits
if (string){
if (string == '1'){
mpprice='22.80';
}
else if (string <= 24 ){
mpprice='22.08';
}
else if (string <= 49 ){
mpprice='21.36';
}
else if (string <= 99 ){
mpprice='19.93';
}
else if (string <= 249 ){
mpprice='18.49';
}
else if (string >= 250){
mpprice='17.06';
}
}
document.getElementById('matprice').innerHTML='$'+mpprice+' each';
}/*]]>*/
</script></head>

<body>
<span id="matprice">$22.80 each</span>
<input type="text" maxlength="5" value="1" id="quantity-input" onchange="mpdprice();">
</body>

</html>