Jump to content
Fórum Script Brasil
  • 0

Buscar valor de array


D31v1d1

Question

Boa noite!

Estou montando um formulário dinamico onde o cliente pode inserir quantos produtos ele quiser, cada produto inserido ele gera este código

<div class="produtos">
    <p class="campoProduto">
    <select name="produto[]" id="produto[]" class="comboselect3">
    <option value="0">- Escolha um produto -</option>
    <?php
    $sp = mysql_query("SELECT * FROM produtos ORDER BY descricao ASC");                
    while($linha = mysql_fetch_array($sp) ){
        echo "<option value='".$linha['id']."'>".$linha['descricao']."</option>";
    }
    ?>
    </select>
    <select name="un" id="un" class="combovp2" onkeyup="soma()">
    <option value="0">- UN -</option>
    <?php
    $sp = mysql_query("SELECT * FROM unidade ORDER BY nomenclatura ASC");                
    while($linha = mysql_fetch_array($sp) ){
        echo "<option value='".$linha['id']."'>".$linha['nomenclatura']."</option>";
    }
    ?>
    </select>
    <input type="text" name="medx[]" id="medx[]" value="0,00" class="combovp2" onkeyup="soma()" /> X 
    <input type="text" name="medy[]" id="medy[]" value="0,00" class="combovp2" onkeyup="soma()" />
    <input type="text" name="qtd[]" id="qtd[]" value="1" class="comboup" onkeyup="soma()" />
    <input type="text" name="valor[]" id="valor[]" value="0,00" class="combovp2" onkeyup="soma()" />
    <input type="text" name="total[]" id="total[]" value="0,00" class="combovp3" disabled />
    <a class="removerCampo" style="cursor:pointer;"><img src="img/remover.png" width="16" height="16" alt="Remover" title="Remover Produto" /></a>
    </p>
    </div>
e este codigo js abaixo faz o calculo dos inputs e apresenta no input total;
addEvent = function(o, e, f, s) {
   var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c, d;
   r[r.length] = [f, s || o], o[e] = function(e){
      try{
         (e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
         e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});
         e.target || (e.target = e.srcElement || null);
         e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
      }catch(f){}
      for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));
      return e = null, !!d;
    }
};

function formatCurrency(o, n, dig, dec) {
   o.c = !isNaN(n) ? Math.abs(n) : 2;
   o.dec = typeof dec != "string" ? "," : dec, o.dig = typeof dig != "string" ? "." : dig;
   addEvent(o, "keypress", function(e) {
      if (e.key > 47 && e.key < 58) {
         var o, s, l = (s = ((o = this).value.replace(/^0+/g, "") + String.fromCharCode(e.key)).replace(/\D/g, "")).length, n;
         if(o.maxLength + 1 && l >= o.maxLength) return false;
         l <= (n = o.c) && (s = new Array(n - l + 2).join("0") + s);
         for(var i = (l = (s = s.split("")).length) - n; (i -= 3) > 0; s[i - 1] += o.dig);
         n && n < l && (s[l - ++n] += o.dec);
         o.value = s.join("");
      }
      e.key > 30 && e.preventDefault();
   });
}

function fmtMoney(n, c, d, t){
   var m = (c = Math.abs(c) + 1 ? c : 2, d = d || ",", t = t || ".",
      /(\d+)(?:(\.\d+)|)/.exec(n + "")), x = m[1].length > 3 ? m[1].length % 3 : 0;
   return (x ? m[1].substr(0, x) + t : "") + m[1].substr(x).replace(/(\d{3})(?=\d)/g,
      "$1" + t) + (c ? d + (+m[2] || 0).toFixed(c).substr(2) : "");
}
function soma() {
    checkx = 0;
    for(i = 0; i < document.ordem.elements.length; i++){
        var check = document.ordem.elements[i];
        if(check.name == "un[]"){
            if(check.checked == true){
                unidade = parseFloat(document.ordem.un[i].value);
                if(unidade==0 || unidade==2) {
                    var f = document.forms[0];
                    var valor1 = parseFloat(f.valor[i].value.replace('.','').replace(',','.'));
                    var valor2 = parseFloat(f.qtd[i].value);
                    var valor3 = ('' + ((valor1||0) * (valor2||0)));
                    f.total.value = fmtMoney(valor3);
                }
                if(unidade==1 || unidade==3) {
                    var f = document.forms[0];
                    var medx = parseFloat(f.medx[i].value.replace('.','').replace(',','.'));
                    var medy = parseFloat(f.medy[i].value.replace('.','').replace(',','.'));
                    var valor1 = parseFloat(f.valor[i].value.replace('.','').replace(',','.'));
                    var valor2 = parseFloat(f.qtd[i].value);
                    var total1 = ('' + ((medx||0) * (medy||0)));
                    var total = ('' + ((total1||0) * (valor1||0)));
                    var valor3 = ('' + ((total||0) * (valor2||0)));
                    f.total.value = fmtMoney(valor3);
                }
            }
        }
    }
}
function init() {
    checkx = 0;
    for(i = 0; i < document.ordem.elements.length; i++){
        var check = document.ordem.elements[i];
        if(check.name == "un[]"){
            if(check.checked == true){
                formatCurrency(document.forms[0].valor[i], 2, ".", ",");
                formatCurrency(document.forms[0].medx[i], 2, ".", ",");
                formatCurrency(document.forms[0].medy[i], 2, ".", ",");
            }
        }
    }
}
window.onload = init;

até ai tudo certo, mas agora quando jogo no name dos inputs por exemplo "un[]" acrescento o [] para criar os array e pegar via js para fazer o calculo de cada produto separado não funciona, alguém pode me ajudar?

tentei

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...