Vi spiego il mio problema... ho un form in cui riproduco il classicco carrello della spesa, voglio usare due bottoni che incrementano e decrementano le quantita di un relativo articolo...
Come spiegato in un'altra discussione presa da questo forum, ho fatto questo... e tutto funziona...
Codice PHP:
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento senza titolo</title>
<script type="text/javascript" language="javascript">
function upanddown(op){
var c=document.preventivo.quantita;
var v=parseInt(c.value);
if(isNaN(v)){alert('Inserire un valore numerico nel campo '+c.name+'.'); return;}
if(op=='+') v++;
if(op=='-' && v>0) v--;
c.value=v;
}
</script>
</head>
<body>
<form name="preventivo">
<input type="text" name="quantita" value="1"/>
<input type="button" value="+" name="b1" onclick="upanddown(this.value);"/>
<input type="button" value="-" name="b1" onclick="upanddown(this.value);"/>
</form>
</body>
</html>
Il problema però sta nel fatto che io non ho solo un campo quantità per articolo, quindi pur non amando molto javascript, sto cercando di adattare questo form in modo che gestisca il tutto in modo dinamico, avevo pensato a questo... ma mi restituisce un errore che non capisco ma a rigor di logica dovrebbe essere giusto... qualcuno mi sa aiutare e dire dove sbaglio, oppure consigliare qualche altra alternativa?
Codice PHP:
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento senza titolo</title>
<script type="text/javascript" language="javascript">
function upanddown(op,elemento){
var c=document.preventivo.elemento;
var v=parseInt(c.value);
if(isNaN(v)){alert('Inserire un valore numerico nel campo '+c.name+'.'); return;}
if(op=='+') v++;
if(op=='-' && v>0) v--;
c.value=v;
}
</script>
</head>
<body>
<form name="preventivo">
<input type="text" name="quantita0" value="1" size="3" />
<input type="button" value="+" name="b1" onclick="upanddown(this.value,'quantita0');"/>
<input type="button" value="-" name="b1" onclick="upanddown(this.value,'quantita0');"/>
<input type="text" name="quantita1" value="1" size="3" />
<input type="button" value="+" name="b1" onclick="upanddown(this.value,'quantita1');"/>
<input type="button" value="-" name="b1" onclick="upanddown(this.value,'quantita1');"/>
</form>
</body>
</html>