thymeleaf html 中 没有权限的人 <input框 文本框不可编辑 不可选择 怎么弄?
时间: 2024-03-21 14:43:06 浏览: 17
你可以使用th:attr属性来实现这个功能,具体步骤如下:
1. 在需要限制权限的input标签中添加th:attr属性。
2. 在th:attr属性中设置disabled属性。
3. 在th:attr属性中使用表达式判断当前用户是否有权限,如果没有权限就设置disabled属性为true,否则设置为false。
例如,假设你想限制只有管理员才能编辑用户信息,那么你可以这样写:
```html
<input type="text" th:attr="${isAdmin} ? '' : 'disabled'" />
```
其中,${isAdmin}是一个表达式,判断当前用户是否是管理员。如果当前用户是管理员,那么disabled属性就为空,可以编辑;否则,disabled属性就为true,不能编辑。
当然,你也可以使用其他方式来实现这个功能,比如在后端控制是否渲染该input标签等。
相关问题
thymeleaf html 中 对<input <textarea <input type="text" 进行不可选 不可编辑的控制 比如这段 <td colspan="1"><input type="text" id="drawPeople" name="drawPeople" th:value="${spotCheck.drawPeople}" class="form-control" placeholder="请输入点检人姓名" autocomplete="off"/> </td>
要对 `input` 和 `textarea` 标签进行不可选和不可编辑的控制,可以添加 `th:attr` 属性和 `th:readonly` 属性,具体方法如下:
1. 不可选
要使 `input` 和 `textarea` 标签变为不可选状态,可以添加 `th:attr` 属性并设置 `disabled` 属性的值为 `disabled`。例如:
```html
<input th:attr="disabled=${true}" type="text" id="drawPeople" name="drawPeople" th:value="${spotCheck.drawPeople}" class="form-control" placeholder="请输入点检人姓名" autocomplete="off"/>
```
这会使 `input` 标签变为不可选状态。同样,对于 `textarea` 标签也可以使用这种方法。
2. 不可编辑
要使 `input` 和 `textarea` 标签变为不可编辑状态,可以添加 `th:readonly` 属性并设置属性值为 `${true}`。例如:
```html
<input th:readonly="${true}" type="text" id="drawPeople" name="drawPeople" th:value="${spotCheck.drawPeople}" class="form-control" placeholder="请输入点检人姓名" autocomplete="off"/>
```
这会使 `input` 标签变为不可编辑状态。同样,对于 `textarea` 标签也可以使用这种方法。
需要注意的是,如果要同时使 `input` 和 `textarea` 标签既不可选又不可编辑,可以同时添加 `th:attr` 和 `th:readonly` 属性。例如:
```html
<input th:attr="disabled=${true}" th:readonly="${true}" type="text" id="drawPeople" name="drawPeople" th:value="${spotCheck.drawPeople}" class="form-control" placeholder="请输入点检人姓名" autocomplete="off"/>
```
这会同时使 `input` 标签既不可选又不可编辑。
th:if="${currentProcess == null || currentProcess.userId != userId}" 加上这段判断 对thymeleaf html 中 对<input <textarea <input type="text" 进行不可选 不可编辑的控制 比如这段 <td colspan="1"><input type="text" id="drawPeople" name="drawPeople" th:value="${spotCheck.drawPeople}" class="form-control" placeholder="请输入点检人姓名" autocomplete="off"/> 这段进行修改。
可以在 `<input>` 标签上添加 `th:readonly` 属性来使其只读,或者添加 `th:disabled` 属性来使其不可编辑。可以根据条件判断是否添加这些属性,例如:
```
<td colspan="1">
<input type="text" id="drawPeople" name="drawPeople" th:value="${spotCheck.drawPeople}" class="form-control" placeholder="请输入点检人姓名" autocomplete="off"
th:if="${currentProcess == null || currentProcess.userId != userId}"
th:readonly="readonly"/>
</td>
```
这样,如果 `currentProcess` 为空或者其 `userId` 不等于 `userId`,则该输入框会变为只读状态,不可编辑。如果要完全禁用该输入框,可以将 `th:readonly` 改为 `th:disabled`。