Visualizzazione dei risultati da 1 a 7 su 7

Discussione: somma array di un form

  1. #1

    somma array di un form

    salve ho qst funzioni:

    function a2cifre(x) {
    var c = String(Math.round(x*100));
    while (c.length < 3) c = '0' + c;
    return c.replace(/([0-9][0-9])$/,".$1");
    }

    function calcola(){


    var qta = document.forms["modulo"].elements["qty[]"];

    var uni = document.forms["modulo"].elements["p_uni[]"];
    var impo = 0;

    for (i=0; i<qta.length; i++) {
    if(isNaN(qta[i].value)) {
    alert("Inserire solo valori numerici, grazie!");
    document.forms["modulo"].elements["qty[]"][i].value = "";
    document.forms["modulo"].elements["qty[]"][i].focus();
    }
    if(isNaN(uni[i].value)) {
    alert("Inserire solo valori numerici, grazie!");
    document.forms["modulo"].elements["p_uni[]"][i].value = "";
    document.forms["modulo"].elements["p_uni[]"][i].focus();
    }

    document.forms["modulo"].elements["p_tot[]"][i].value=a2cifre(qta[i].value*uni[i].value);

    impo=impo+Number(document.forms["modulo"].elements["p_tot[]"][i].value);

    }


    document.modulo.impo.value=a2cifre(Math.round(impo * 100) / 100);
    iva_tot=impo*Number(document.modulo.iva.value)/100;
    document.modulo.iva_tot.value=a2cifre(Math.round(i va_tot * 100) / 100);
    document.modulo.totale.value=a2cifre(impo+Number(d ocument.modulo.iva_tot.value)+Number(document.modu lo.spese.value));
    }

    il problema nasce se i vettori qty[] e p_uni[] sono di un solo elemento...

    ho provato anche con document.getelementsbyname ma non funge.....

    grazie ...

  2. #2
    nessuna risposta!!!

  3. #3
    Moderatore di Annunci siti web, Offro lavoro/collaborazione, Cerco lavoro L'avatar di cavicchiandrea
    Registrato dal
    Aug 2001
    Messaggi
    26,133
    Prova cosi:
    codice:
    if(qta.length>0){
    for (i=0; i<qta.length; i++) {
    if(isNaN(qta[i].value)) {
    alert("Inserire solo valori numerici, grazie!");
    document.forms["modulo"].elements["qty[]"][i].value = "";
    document.forms["modulo"].elements["qty[]"][i].focus();
    }
    if(isNaN(uni[i].value)) {
    alert("Inserire solo valori numerici, grazie!");
    document.forms["modulo"].elements["p_uni[]"][i].value = "";
    document.forms["modulo"].elements["p_uni[]"][i].focus();
    }
    }else{
    document.forms["modulo"].elements["qty[]"][0].value = "";
    document.forms["modulo"].elements["qty[]"][0].focus();
    document.forms["modulo"].elements["p_uni[]"][0].value = "";
    document.forms["modulo"].elements["p_uni[]"][0].focus();
    }
    Cavicchi Andrea
    Problemi con javascript, jquery, ajax clicca qui

  4. #4
    mi dice qta undefined...
    perchè credo che qnd ce ne solo uno non è un array ma una variabile

  5. #5
    Moderatore di Annunci siti web, Offro lavoro/collaborazione, Cerco lavoro L'avatar di cavicchiandrea
    Registrato dal
    Aug 2001
    Messaggi
    26,133
    Posta il link pubblico
    Cavicchi Andrea
    Problemi con javascript, jquery, ajax clicca qui

  6. #6

  7. #7

    Risolto...

    ecco la funzione corretta... per firefox va.. era quello che m'interessava...

    function a2cifre(x) {
    var c = String(Math.round(x*100));
    while (c.length < 3) c = '0' + c;
    return c.replace(/([0-9][0-9])$/,".$1");
    }

    function calcola(){


    var qta = document.forms["modulo"].elements["qty[]"];
    // alert(qta.length);
    var uni = document.forms["modulo"].elements["p_uni[]"];
    var impo = 0;

    if(qta.length=="undefined"){
    for (i=0; i<qta.length; i++) {
    if(isNaN(qta[i].value)) {
    alert("Inserire solo valori numerici, grazie!");
    document.forms["modulo"].elements["qty[]"][i].value = "";
    document.forms["modulo"].elements["qty[]"][i].focus();
    }
    if(isNaN(uni[i].value)) {
    alert("Inserire solo valori numerici, grazie!");
    document.forms["modulo"].elements["p_uni[]"][i].value = "";
    document.forms["modulo"].elements["p_uni[]"][i].focus();
    }

    document.forms["modulo"].elements["p_tot[]"][i].value=a2cifre(qta[i].value*uni[i].value);

    impo=impo+Number(document.forms["modulo"].elements["p_tot[]"][i].value);
    }
    }
    else{
    if(isNaN(qta.value)) {
    alert("Inserire solo valori numerici, grazie!");
    document.forms["modulo"].elements["qty[]"].value = "";
    document.forms["modulo"].elements["qty[]"].focus();
    }
    if(isNaN(uni.value)) {
    alert("Inserire solo valori numerici, grazie!");
    document.forms["modulo"].elements["p_uni[]"].value = "";
    document.forms["modulo"].elements["p_uni[]"].focus();
    }

    document.forms["modulo"].elements["p_tot[]"].value=a2cifre(qta.value*uni.value);

    impo=impo+Number(document.forms["modulo"].elements["p_tot[]"].value);

    }


    document.modulo.impo.value=a2cifre(Math.round(impo * 100) / 100);
    iva_tot=impo*Number(document.modulo.iva.value)/100;
    document.modulo.iva_tot.value=a2cifre(Math.round(i va_tot * 100) / 100);
    document.modulo.totale.value=a2cifre(impo+Number(d ocument.modulo.iva_tot.value)+Number(document.modu lo.spese.value));
    }

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.