Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

435
Visualizações
Dynamic form with Pug (Jade) and JavaScript

I have a form with some static fields and other are generated dynamicly.

The problem is that when I submit the form only the values of static fields are returned to Express. On Express I check the values like this: console.log(req.body); and it shows something like this:

{ staticfoofield1: '',
  staticfoofield2: '',
  staticfoofield3: '',
  staticfoofield4: '',
  }

This is correct for the static fields but dynamic fields are missing!

--> How can I return the values of dynamic fields?


Example of how I created the dynamic fields:

Javascript:

script.
        function addAdjustment(){

            var i = 1;
            if ((document.getElementById("ncolors").value >=1) && (document.getElementById("ncolors").value <=12)){

                while (i <= document.getElementById("ncolors").value){

                    var divElem = document.createElement('div'); 
                        divElem.className = 'col-sm-1';
                        divElem.style.padding = '2px';

                    var inputElem = document.createElement('input');
                        inputElem.id = 'Ajuste' + i;
                        inputElem.setAttribute('placeholder', 'Ajuste' + i);
                        inputElem.setAttribute('type', 'text');
                        inputElem.nodeName = 'Ajuste' + i;
                        inputElem.style.margin = '5px';
                    groupElem.appendChild(inputElem);

                    document.getElementById("adjustments").appendChild(groupElem);
                    i++;
                }
            }
            else{
                alert("The number of colors are not correct!");
            }
        }

Pug (Jade):

        div.row
            div.col-sm-5
            div.col-sm-2
                h6 Ajustes Colores
            div.col-sm-5 

        div.row(id="ajustments") 

        div.row
            div.col-sm-4
            div.col-sm-2
                div.form_group
                    div.actions
                        input.button(type="button", value="Add Adjust", onclick="addAdjustment()")
            div.col-sm-2
                div.form_group
                    div.actions
                        input.button(type="button", value="Delete Adjust", onclick="deleteAdjustment()")
            div.col-sm-4

Router:

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Tolerancias' });
});

router.post('/', function (req, res) {
    console.log("Form Data: " + req.body);
    res.send('index');
});

module.exports = router;
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

In order to be included in a form submission an input needs a name and a value as that is what is passed with the request. You'll need to modify your code to something like this:

var inputElem = document.createElement('input');
                    inputElem.id = 'Ajuste' + i;
                    inputElem.setAttribute('name', 'Ajuste' + i);
                    inputElem.setAttribute('value', 'Ajuste' + i);
                    inputElem.setAttribute('placeholder', 'Ajuste' + i);
                    inputElem.setAttribute('type', 'text');
                    inputElem.nodeName = 'Ajuste' + i;
                    inputElem.style.margin = '5px';
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda