• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

269
Vistas
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

almost 3 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

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.

almost 3 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda