Thymeleaf 根据点击设置对象属性

Thymeleaf setting object attribute based on click

我正在尝试根据点击设置对象用户变量属性。

`

 <form class="container" th:action="@{/processSignup}" method="post" 
   th:object="${user}">

    <div class="switch">
        <div class="MenteeSignUp" onclick="tab1();" th:onclick="*{}"  >Mentee</div>
        <div class="MentorSignUp" onclick="tab2();" th:value="MENTOR" th:field="*{userRole}">Mentor</div>
    </div>

`

尝试添加不同的基于角色的用户单击您可以从屏幕截图中看到的导师或受训者。

我对百里香叶有点陌生,所以我尝试 th:onClick 然后尝试分配它但它没有用

form

你那里的代码没有任何意义。 <div /> 没有 value 属性,并且 th:onclick 中的表达式必须有效 javascript(相反,您有一个空白的选择变量表达式:th:onclick="*{}" ).也许您正在寻找这样的东西?

<form class="container" th:action="@{/processSignup}" method="post" th:object="${user}">
    <input type="hidden" th:field="*{userRole}" id="userRole" />
    
    <div class="switch">
        <div class="MenteeSignUp" onclick="document.getElementById('userRole').value = 'MENTEE';">Mentee</div>
        <div class="MentorSignUp" onclick="document.getElementById('userRole').value = 'MENTOR';">Mentor</div>
    </div>