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

287
Visualizações
How can i run an <input type="number"> onchange event using thymeleaf?

Hello,

i have a little problem that maybe you could help me with. I searched the internet for quite a long time but i didn't find any answers.

I have a little web shop using Spring and Thymeleaf. My task is now to implement the option to change the quantity of a cart item inside the cart. This value is stored in a variable ${item.quantity}.

So in conclusion if I press "up" or "down" on the input field, the item in the cart should change its quantity and the total price of all cart items should be evaluated again.

I used an <input type="number"> combined with an onchange event running a javascript function, but all my tries went wrong.

Here is the code snippet of cart.html template:

<input 
  type="number"
  onchange="change(this.value)"
  min="1"
  max="5" 
  th:value="${item.quantity}"
>

And this is my javascript code:

<script th:inline="javascript">
/*<![CDATA[*/
function change(data) {
  var quantity = /*[[${item.quantity}]]*/ data;
  location.reload(false);
}
/*]]>*/
</script>

but this doesn't work.

Hopefully you understand what I mean and someone can help me, because I really don't have any idea how to else do this.

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

0

You need to send the changed quantity back to the server. As a first step do it without JavaScript. Use a simple form, for example:

<form action="/change-quantity">
  <input type="hidden" name="itemId" th:value="${item.id}">
  <input type="number" min="1" max="5" th:value="${item.quantity}">
  <input type="submit" value="Update">
</form>

Now you need to write a POST controller for the path /change-quantity that looks up the item by the ID, change it's quantity and finaly redirects back to the page you came from.

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