How to Set Thymeleaf Th:Field Value from Other Variable

how to set th:field thymeleaf from javascript variable

i found the solution i add in the javascript code :

element2.name="numberOfAdults"; // when i create the dropdown dynamically

........

on the function where i get the value i add this code :

          function sumOppositeWalls(nbrA){

var table = document.getElementById('myTable2');
var rowCount = table.rows.length;
//alert(rowCount);
var minusCount=0;
for(var i=1;i<rowCount;i++){
nbrA=document.getElementById('id1_'+i).value;
nbrE=document.getElementById('id2_'+i).value;
$('#numberOfAdults').val()=nbrA;
$('#numberOfChildren').val()=nbrb;
alert(nbrA);


}

how to enable a input field (th:field) and set a value(th:value) at the same time

The feld pres should be initialized before the form is rendered.

// In the controller
formObject.setPres(/* however you get authentication.name in the controller */);

Then your input will be initialized to the correct value when you use th:field.

<input type="text" th:field="*{pres}" />

Is it possible to use th-field to retrieve the value of a list in Thymeleaf?

You can only use th:field if you are using th:object in a parent <form /> tag. (Which isn't clear from the HTML you posted -- but you probably are not since your are adding valoresAtributo directly as a model attribute.)

If you wish to show a preselected <option> but without using th:object and th:field you should instead use the th:selected attribute which should evaluate to true or false based on if that option should be selected. It should look something like this:

<select class="form-control col-md-10">
<option
th:each="option : ${T(com.jequiti.JequitiIntegrador.controller.AtributoController).test(valorAtributo.sqlValidacao)}"
th:value="${{option.valorAtributo}}"
th:text="${option.significadoAtributo}"
th:selected="${valorAtributo.valorUsuario == option.valorAtributo}" />
</select>

Thymeleaf: Partially set values to model object from outside the form tags

Use a hidden input in your form:

<form th:object="${participant}" method="post">
<input type="text" th:field="*{username}" >
<input type="hidden" th:field="*{taskCompleted}" />
<button type="submit">Join!</button>
</form>

When the user clicks a button, use JavaScript to set the value of the hidden input to true.

<!-- This button flips the value of taskCompleted to true -->
<button onclick="document.getElementById('taskCompleted').value = 'true';">Do the task first!</button>


Related Topics



Leave a reply



Submit