Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it
    Registrato dal
    May 2002
    Messaggi
    2,929

    function

    ciao a tutti,

    sto cercando di risolvere un problema da un pò di giorni,

    ho una funzione che mi calcola i prezzi in base a due parametri

    1) il numero di pezzi
    2) se la checkbox e spuntata

    non sono lontana dalla soluzione ma sto sicuramente facendo degli errori concettuali e non riesco a venirne fuori, mi dareste una mano la pagina è piccola se volete potete ricostruirla copiando qui

    codice:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>CALCOLO CHEKBOX</title>
    <script type="text/javascript">
    <!--
    
    function resettacheck (){
     var e=document.form1.elements;
     for(var i=0;i<e.length;i++) 
     if(e[i].name.indexOf('selezione')>-1 && e[i].type=='checkbox' && e[i].checked) 
     	e[i].checked=false;
    	document.form1.sez.value = "0";
    }
    function disabilita (quale){
     var s=0;
     var e=document.form1.elements;
     var pz = parseInt(e['nart'+quale].value, 10); 
     var prezzo = parseFloat(e['selezione'+quale].value, 10); 
     var totale = parseFloat(document.form1.sez.value, 10); 
    	if (e['selezione'+quale].checked==true && totale != 0 && pz == 0){
    	e['selezione'+quale].checked=false;
    	document.form1.sez.value -= e['selezione'+quale].value;
    	document.form1.sez.value = Math.round(document.form1.sez.value*100) / 100;
    	e['selezione'+quale].disabled = false;
    	
    	}else if (e['selezione'+quale].checked==true && totale != 0 && pz != 0) {
    	e['selezione'+quale].checked=false;
    	var diviso = e['selezione'+quale].value * e['nart'+quale].value;
    	document.form1.sez.value -= diviso;
    	document.form1.sez.value = Math.round(document.form1.sez.value*100) / 100;
    	e['selezione'+quale].disabled = false;
    	}
    }
    
    function somma(quale){
     var s=0;
     var e=document.form1.elements;
     var pz = parseInt(e['nart'+quale].value, 10); 
     var prezzo = parseFloat(e['selezione'+quale].value, 10); 
     var totale = parseFloat(document.form1.sez.value, 10); 
     
     if (pz != 0) {
    	var tot = prezzo*pz;
    	s =tot+totale;
     	s= Math.round(s*100) / 100;
     	alert(s);
     	document.form1.sez.value = s;
     	e['selezione'+quale].disabled = true;
     	}else{
     	for(var i=0;i<e.length;i++)
     	if(e[i].name.indexOf('selezione')>-1 && e[i].type=='checkbox' && e[i].checked)
     	s +=parseFloat(e[i].value);
     	s= Math.round(s*100) / 100;
     	alert(s);
     	document.form1.sez.value = s;
     	e['selezione'+quale].disabled = true;
     }
    }
    
    //-->
    </script>
    <style type="text/css">
    <!--
    .Stile3 {color: #FFFFFF}
    .Stile21 {color: #FF0000; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; }
    .Stile22 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; }
    .Stile23 {font-size: 11px}
    .Stile25 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; color: #000000;}
    -->
    </style>
    </head>
    
    <body>
    <form name="form1" method="post" action="">
      <table width="200" border="0" cellpadding="5" cellspacing="15">
        <tr>
          <td nowrap><span class="Stile21">PZ.
                <input name="nart22" type="text" id="nart22" onFocus="disabilita (22)" value="0" size="5" maxlength="4"> &euro; 18.64</span>
              <span class="Stile22">
              <input name="selezione22" type="checkbox" id="selezione22" onClick="return somma(22)" value="18.64">
              </span></td>
          <td nowrap><span class="Stile21">PZ.
                <input name="nart60" type="text" id="nart60" onFocus="disabilita (60)" value="0" size="5" maxlength="4"> &euro; 17.13</span>
              <span class="Stile22">
              <input name="selezione60" type="checkbox" id="selezione60" onClick="return somma(60)" value="17.13">
          </span></td>
        </tr>
        <tr>
          <td colspan="2" nowrap><span class="Stile23"></span><span class="Stile23"></span></td>
        </tr>
        <tr>
          <td colspan="2" nowrap><div align="right"><span class="Stile3"><span class="Stile25">TOTALE ARTICOLI &euro;</span></span><span class="Stile3"><span class="Stile25">
            <input name="sez" type="text" id="sez" value="0" size="6" maxlength="6">
          </span></span></div>        </td>
        </tr>
        <tr>
          <td colspan="2" nowrap><div align="right"><span class="Stile3"><span class="Stile25">RESET FORM </span></span></div>        <div align="left"><span class="Stile3"><span class="Stile25"></span></span></div></td>
        </tr>
      </table>
    </form>
    </body>
    </html>
    Vi pregodatemi una manina


    Grazie

  2. #2
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    a me sembra funzioni dove è il problema?
    Pietro

  3. #3
    Utente di HTML.it
    Registrato dal
    May 2002
    Messaggi
    2,929
    se provi ad aggiungere più pezzi e poi li togli oppure togli tutto i calcoli sballano...


    allora fai cosi

    aggiungi due pezzi al primo articolo pio aggiungi il secondo il risutato è errato!

    e per di più quando fai reset i campi dei pezzi non si azzerano


    :master: grazie

  4. #4
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    codice:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>CALCOLO CHEKBOX</title>
    <script type="text/javascript">
    <!--
    
    function resettacheck (){
     var e=document.form1.elements;
     for(var i=0;i<e.length;i++) {
       if(e[i].name.indexOf('selezione')>-1 && e[i].type=='checkbox' && e[i].checked) {
     	e[i].checked=false;
            e[i].disabled=false;
       }
       if (e[i].name.indexOf('nart')>-1) {
         e[i].value="";
       }
     }
     document.form1.sez.value = "0";
    }
    function disabilita (quale){
     var s=0;
     var e=document.form1.elements;
     var pz = parseInt(e['nart'+quale].value, 10); 
     var prezzo = parseFloat(e['selezione'+quale].value, 10); 
     var totale = parseFloat(document.form1.sez.value, 10); 
    	if (e['selezione'+quale].checked==true && totale != 0 && pz == 0){
    	e['selezione'+quale].checked=false;
    	document.form1.sez.value -= e['selezione'+quale].value;
    	document.form1.sez.value = Math.round(document.form1.sez.value*100) / 100;
    	e['selezione'+quale].disabled = false;
    	
    	}else if (e['selezione'+quale].checked==true && totale != 0 && pz != 0) {
    	e['selezione'+quale].checked=false;
    	var diviso = e['selezione'+quale].value * e['nart'+quale].value;
    	document.form1.sez.value -= diviso;
    	document.form1.sez.value = Math.round(document.form1.sez.value*100) / 100;
    	e['selezione'+quale].disabled = false;
    	}
    }
    
    function somma(quale){
     var s=0;
     var e=document.form1.elements;
     var pz = parseInt(e['nart'+quale].value, 10); 
     var prezzo = parseFloat(e['selezione'+quale].value, 10); 
     var totale = parseFloat(document.form1.sez.value, 10); 
     
     if (pz != 0) {
    	var tot = prezzo*pz;
    	s =tot+totale;
     	s= Math.round(s*100) / 100;
     	alert(s);
     	document.form1.sez.value = s;
     	e['selezione'+quale].disabled = true;
     	}else{
     	for(var i=0;i<e.length;i++)
     	if(e[i].name.indexOf('selezione')>-1 && e[i].type=='checkbox' && e[i].checked)
     	s +=parseFloat(e[i].value);
     	s= Math.round(s*100) / 100;
     	alert(s);
     	document.form1.sez.value = s;
     	e['selezione'+quale].disabled = true;
     }
    }
    
    //-->
    </script>
    <style type="text/css">
    <!--
    .Stile3 {color: #FFFFFF}
    .Stile21 {color: #FF0000; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; }
    .Stile22 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; }
    .Stile23 {font-size: 11px}
    .Stile25 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; color: #000000;}
    -->
    </style>
    </head>
    
    <body>
    <form name="form1" method="post" action="">
      <table width="200" border="0" cellpadding="5" cellspacing="15">
        <tr>
          <td nowrap><span class="Stile21">PZ.
                <input name="nart22" type="text" id="nart22" onFocus="disabilita (22)" value="0" size="5" maxlength="4"> € 18.64</span>
              <span class="Stile22">
              <input name="selezione22" type="checkbox" id="selezione22" onClick="return somma(22)" value="18.64">
              </span></td>
          <td nowrap><span class="Stile21">PZ.
                <input name="nart60" type="text" id="nart60" onFocus="disabilita (60)" value="0" size="5" maxlength="4"> € 17.13</span>
              <span class="Stile22">
              <input name="selezione60" type="checkbox" id="selezione60" onClick="return somma(60)" value="17.13">
          </span></td>
        </tr>
        <tr>
          <td colspan="2" nowrap><span class="Stile23"></span><span class="Stile23"></span></td>
        </tr>
        <tr>
          <td colspan="2" nowrap><div align="right"><span class="Stile3"><span class="Stile25">TOTALE ARTICOLI €</span></span><span 
    
    class="Stile3"><span class="Stile25">
            <input name="sez" type="text" id="sez" value="0" size="6" maxlength="6">
          </span></span></div>        </td>
        </tr>
        <tr>
          <td colspan="2" nowrap><div align="right"><span class="Stile3"><span class="Stile25">RESET FORM </span></span></div>        <div align="left"><span class="Stile3"><span class="Stile25"></span></span></div></td>
        </tr>
      </table>
    </form>
    </body>
    </html>
    Ricomponi la parola javascript in fondo, al link di reset
    Ciao
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  5. #5
    Utente di HTML.it
    Registrato dal
    May 2002
    Messaggi
    2,929
    Grazie milleeeeeeeeee


    PERFETTO!!!!


Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.