我们知道要想通过th:field取值,前面必须要有th:object; th:object表示获取的表单或者后台数据对象;th:field只是从该对象中取出响应的属性;
<form id="form1" th:object="${animal}">
<input id="partId" name="partId" th:field="*{age}"/>
</form>
其中的*{age}取的就是animal对象中的age属性所对应的值
当你想在js文件中获取th:field的值:
[[${th:Object的值.th:field的值}]]
例如:
[[${animal.age}]]
当你想直接在<input>中通过函数传参,其中一个参数是th:field的值的情况:
需要先写一个th:value=${th:field中的值},然后在通过attr()获取该value属性的值:
<form id="form1" th:object="${animal}">
<input id="animalAge" name="animalAge" th:field="*{age}" th:value="${age}"
onclick="function getAge(){alert($('animalAge').attr('value'))}"/>
</form>
注意:onclick()中的attr()中字符就是value;
因为th:value在编译后会被编译成 value;
由于th:field在编译后会被编译成name,所以不能通过attr('th:field')获取到;