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

338
Visualizações
is there a way to run javaScript in th:action method thymeleaf

This is my form:-

<form action="#" th:object="${note}" th:action="$('#note-id').val()!=null ? @{/update} : @{/note}" method="POST">
    <input type="hidden" name="noteId" id="note-id" th:field="*{noteId}"  >
    <div class="form-group">
       <label for="note-title" class="col-form-label">Title</label>
       <input type="text" name="noteHead" th:field="*{noteHead}"  class="form-control" id="note-title" maxlength="20" required>
    </div>
    <div class="form-group">
       <label for="note-description" class="col-form-label">Description</label>
       <textarea class="form-control" name="noteDescription" th:field="*{noteDescription}" id="note-description" rows="5" maxlength="1000" required></textarea>
   </div>
   <button id="noteSubmit" type="submit" class="d-none" ></button>
</form>

This form is inside a popUp window(modal class). I want use the same Window for add/update. Add is working fine.

For Update i am passing in the data to the modal, which is getting displayed aswell.

<button type="button" class="btn btn-success"
    th:data-noteId="${note.noteId}"
    th:data-noteTitle="${note.noteHead}"
    th:data-noteDescription="${note.noteDescription}"
    onclick="showNoteModal(
    this.getAttribute('data-noteId'),
    this.getAttribute('data-noteTitle'),
    this.getAttribute('data-noteDescription'));">
Edit</button>

What i want to do is to create a conditional statement in th:action based on wheather "note-id" has value, in which case its an update otherwise it should be a New Entry.

Please Help

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

0

You want to update th:action of your form in such a way that if note-id has value an edit request is sent to server otherwise add request?

If that's the case then you can do it this way

<form th:action="${user.note_id}==null? @{/save} : @{/edit}">

With param

<form th:action="${user.note_id}==null? @{/save} : @{/edit/(noteId=${user.note_id})}">


Check thymeleaf standard URL syntax documentation for more details.

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