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

246
Visualizações
Js function argument depend upon the submitted value in EJS

I am building an express app. I have a drop-down whose submitted value is passed to users.js and initiates a mongodb query. Then, result of that query is passed back to ejs and becomes an argument of a script function.

The problem is I want that function to be called when I click on Submit. (I can afford the delay in rendering of page).

Right now, I have two buttons, one for submit and one for calling that function.

index2.ejs


    <form action="/users/details" method="POST">
      <fieldset>
      <select required class="selectpicker" name="selectpicker">
            <!-- options -->
        </select>
        
      <input type="submit" id="submit-form" />
      </fieldset>
  </form>


  <button onclick="popu(<%= JSON.stringify(coun) %>);">blue</button>

and users.js

router.get("/index2", function(req, res, next) {

  (async function() {
      count = await funcoun(coll); // a mongodb query
      res.render("index2", {title: 'some data', userdataa: a, imgsrc : coll, coun : count});
    }) ();
});

router.post("/details", function(req, res, next) {
    coll = req.body.selectpicker;
    res.redirect("/users/index2");
})

So, basically I want the form to pass value (of coll) to express, calculation of count, calling of function with this value, and then rendering the page.

Currently, calculation of count is done on basis of old value, and when blue button is clicked then present value.

I am confused that if I put onclick in submit button, what will it do since coun is not yet calculated.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

<body onload = "popu(<%= JSON.stringify(coun) %>);">

solved the issue

about 4 years ago · Juan Pablo Isaza 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