Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it
    Registrato dal
    Dec 2012
    Messaggi
    201

    Somma di un campo in tabella generata dinamicamente

    Ciao a tutti,
    ho una tabella nella quale genero le righe con uno script, dovrei però fare la somma di uno specifico campo, solo che ottengo [object HTMLElement] come errore. Chi mi sà dare un input? Allego lo script...
    Grazie

    codice:
    var num=1;
    function accoda(){
        if(document.createElement && document.getElementById && document.getElementsByTagName) {
            // crea elementi
            var oTr=document.createElement("TR");
            oTr.setAttribute("class","product");
    
    
            var oTd1=document.createElement("TD");
            var oTd2=document.createElement("TD");
    
    var oField=document.createElement("INPUT");
     $(function() {
        $(oField).autocomplete({
            source: "cerca_oggetto.php",
        					});
    
    	});
    
    	  var oFieldB =document.createTextNode(" ");
    		  var oFieldC=document.createElement("INPUT"); 
    	 var oFieldD =document.createTextNode(" ");	  
    		  var oFieldE=document.createElement("INPUT"); 
    	 var oFieldF =document.createTextNode(" \u20AC \u00A0 \u00A0");	  
    		  var oFieldG=document.createElement("INPUT"); 
    	var oFieldH =document.createTextNode("\u00A0 \u00A0 Totale: ");
    		  var oFieldI=document.createElement("INPUT");
    	var oFieldL =document.createTextNode(" \u20AC ");	  
    
    		  var oButt=document.createElement("INPUT");
    
    
    
            // setta attributi
                    oField.setAttribute("type","text");
    		oField.setAttribute("name","codice_"+num);
    		oField.setAttribute("id","codice_");
    		oField.setAttribute("placeholder","Nome Prodotto");
    		oField.setAttribute("size","30");
                    oField.setAttribute("class","codice");	
    		oField.setAttribute("onchange", "myFunction(this)");
    
                    oFieldB.setAttribute("type","text");
                    oFieldB.setAttribute("name","un_misura"+num);
    		oFieldB.setAttribute("id","un_misura_");
    		oFieldB.setAttribute("placeholder","Un. Misura");
                    oFieldB.setAttribute("readonly","");
    		oFieldB.setAttribute("size","10");
    		oFieldB.setAttribute("class","un_misura");
    		oFieldB.setAttribute("style","text-align:center");
    
    		oFieldC.setAttribute("type","text");
                    oFieldC.setAttribute("name","prezzo_"+num); 
    		oFieldC.setAttribute("id","prezzo_"); 
    		oFieldC.setAttribute("placeholder","Prezzo'");
    		oFieldC.setAttribute("size","8");
                    oFieldC.setAttribute("class","prezzo");
    		oFieldC.setAttribute("style","text-align:right");
    
    
    		oFieldD.setAttribute("type","text");
                    oFieldD.setAttribute("name","qta_"+num);
    		oFieldD.setAttribute("id","qta_"); 
    		oFieldD.setAttribute("placeholder","Quantita'");
    		oFieldD.setAttribute("size","8");
                    oFieldD.setAttribute("class","qta");
    		oFieldD.setAttribute("onkeyup", "calc(this)");
    		oFieldD.setAttribute("style","text-align:center");
    
    		oFieldE.setAttribute("type","text");
                    oFieldE.setAttribute("name","tot"+num);
    		oFieldE.setAttribute("id","tot_"); 
    		oFieldE.setAttribute("placeholder","Tot'");
    		oFieldE.setAttribute("size","8");
                    oFieldE.setAttribute("readonly","");
    		oFieldE.setAttribute("class","tot");
    		oFieldE.setAttribute("style","text-align:right");
    		oFieldE.setAttribute("onkeyup", "total(this)");
    
            oButt.setAttribute("type","button");
    		oButt.setAttribute("class","btn btn-danger");
            oButt.setAttribute("value","rimuovi");
    
    
            // setta gestore evento
            if(oButt.attachEvent) oButt. attachEvent('onclick',function(e){rimuovi(e);})
            else if(oButt.addEventListener) oButt. addEventListener('click',function(e){rimuovi(e);},false)
    
    
            // appendi al relativo padre
            oTd1.appendChild(oField);
            oTd1.appendChild(oFieldB); 
    	oTd1.appendChild(oFieldC);
           	oTd1.appendChild(oFieldD);
    	oTd1.appendChild(oFieldE);
    
            oTd2.appendChild(oButt);
            oTr.appendChild(oTd1);
            oTr.appendChild(oTd2);
            document.getElementById('tabella').getElementsByTagName('TBODY')[0].appendChild(oTr);
    
    var tabella =0;
    $('tabella').each(function(){
    
    
    tot_ricetta +=Number($(this).children('.tot').val());
    
    
    
    
    });
    
    
    
    		alert (tot_ricetta); 
    		$("#tot_ricetta").html(tot_ricetta);
    
    
    	document.getElementById('iterazioni_form_aggiuntivi').value = num;
            // incrementa variabile globale
            num++
        }
    
    
    }
    
    
    
    
    function rimuovi(e){
        if(document.removeChild && document.getElementById && document.getElementsByTagName) {
            if(!e) e=window.event;
            var srg=(e.target)?e.target:e.srcElement;
    
    
            // risali al tr del td che contiene l' elemento che ha scatenato l' evento
            while(srg.tagName!="TR"){srg=(srg.parentNode)?srg.parentNode:srg.parentElement}
    
    
            // riferimento al tbody
            var tb=document.getElementById('tabella').getElementsByTagName('TBODY')[0];
    
            // rimuovi
            tb.removeChild(srg);
        }
    }
    

  2. #2
    Moderatore di Annunci siti web, Offro lavoro/collaborazione, Cerco lavoro L'avatar di cavicchiandrea
    Registrato dal
    Aug 2001
    Messaggi
    26,131
    Qui c’è un errore
    codice:
    $('tabella').each(function(){

    da quelllo che ho capito tabella è un id in tal caso in query sarà #tabella. Inoltre non ripetere la parola tabella sia come id che come variabile.


    Cavicchi Andrea
    Problemi con javascript, jquery, ajax clicca qui

  3. #3
    Utente di HTML.it
    Registrato dal
    Dec 2012
    Messaggi
    201
    Ciao Andrea,
    come sempre grazie per le risposte!
    Ho appena provato con:
    codice:
    $('#tabella').each(function(){
    La risposta è un:
    [object HTMLElement]NaN ...

  4. #4
    Utente di HTML.it
    Registrato dal
    Dec 2012
    Messaggi
    201
    Ho cambiato lo script...

    ho inserito in cima allo script
    codice:
    var tot_ricetta =0;
    var num=1; 
    ...
    
    $("#tabella").each(function(){
    tot_ricetta += Number($(this).children('.tot').val());
    });
    
    ...
    Ottengo come valore NaN ... Cosa sbaglio???

  5. #5
    Utente di HTML.it
    Registrato dal
    Dec 2012
    Messaggi
    201
    Diciamo che sono riusciuto, in parte con il mio script, però ora sono davanti ad un problema... In caso rimuova una riga come posso aggiornare la somma?
    Il codice è questo:

    codice:
    var tot_ricetta=0; 
     function total(element){
    
    
    var medio=new Number($(element).closest('.product').find('.tot').val());
     tot_ricetta+=medio;
     $("#totale_ricetta").html(parseFloat(tot_ricetta).toFixed(2));
     }
    Qualcuno mi sa dare una dritta?

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 © 2024 vBulletin Solutions, Inc. All rights reserved.