关键词搜索

源码搜索 ×
×

前端笔记-利用thymeleaf隐藏显示input及label

发布2020-03-12浏览13489次

详情内容

先来看看效果:

这里有一个add.html,添加,修改都使用这个文件,当点击添加后:

add.html被构造成这样(点击添加后):

当点击编辑后:

对应的前端源码如下:

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <link rel="stylesheet" href="https://cdn.jsdelivr.net/semantic-uihttps://cdn.jxasp.com:9143/image/2.2.4/semantic.min.css" />
  5. <link rel="stylesheet" type="text/css" href="#" th:href="@{/bg/my.css}" />
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  7. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
  8. <meta name="description" content="" />
  9. <meta name="author" content="" />
  10. <title>考勤管理系统</title>
  11. <!-- Bootstrap core CSS -->
  12. <link href="asserts/css/bootstrap.min.css" th:href="@{/webjars/bootstrap/4.0.0/css/bootstrap.css}" rel="stylesheet" />
  13. `
  14. <!-- Custom styles for this template -->
  15. <link href="asserts/css/dashboard.css" th:href="@{/asserts/css/dashboard.css}" rel="stylesheet" />
  16. <style type="text/css">
  17. /* Chart.js */
  18. @-webkit-keyframes chartjs-render-animation {
  19. from {
  20. opacity: 0.99
  21. }
  22. to {
  23. opacity: 1
  24. }
  25. }
  26. @keyframes chartjs-render-animation {
  27. from {
  28. opacity: 0.99
  29. }
  30. to {
  31. opacity: 1
  32. }
  33. }
  34. .chartjs-render-monitor {
  35. -webkit-animation: chartjs-render-animation 0.001s;
  36. animation: chartjs-render-animation 0.001s;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <div class="container-fluid">
  42. <div class="row">
  43. <!-- 引入sidebar -->
  44. <div th:replace="commons/bar::#sidebar(activeUri='super')"></div>
  45. <main class="main ui container">
  46. <div class="ui grid">
  47. <div class="one wide column"></div>
  48. <div class="fifteen wide column">
  49. <form th:action="@{/admin/add}" method="post">
  50. <input type="hidden" name="_method" value="put" th:if="${people!=null}"/>
  51. <input type="hidden" name="id" th:if="${people!=null}" th:value="${people.id}"/>
  52. <div class="form-group">
  53. <label>用户名</label>
  54. <input name="account" type="text" class="form-control" placeholder="root"
  55. th:value="${people!=null}?${people.id}" th:readonly="${people!=null} ? 'true':'false'" />
  56. </div>
  57. <div class="form-group">
  58. <label>密码</label>
  59. <input name="password" type="password" class="form-control" placeholder="123456"
  60. th:value="${people!=null}?${people.pwd}" />
  61. </div>
  62. <div class="form-group">
  63. <label>是否激活</label><br/>
  64. <div class="form-check form-check-inline">
  65. <input class="form-check-input" type="radio" name="active" value="1"
  66. th:checked="${people!=null}?${people.active==1}" />
  67. <label class="form-check-label"></label>
  68. </div>
  69. <div class="form-check form-check-inline">
  70. <input class="form-check-input" type="radio" name="active" value="0"
  71. th:checked="${people!=null}?${people.active==0}" />
  72. <label class="form-check-label"></label>
  73. </div>
  74. </div>
  75. <div class="form-group">
  76. <label th:style="${people==null} ? 'display: none' : ''">修改时间</label>
  77. <input th:type="${people!=null} ? 'text' : 'hidden'" class="form-control" placeholder="https://cdn.jxasp.com:9143/image/2020/3/12 "
  78. th:value="${people!=null}?${#dates.format(people.modifyTime, 'yyyy-MM-dd hh:mm:ss')}" readonly="readonly" />
  79. </div>
  80. <div class="form-group">
  81. <label th:style="${people==null} ? 'display: none' : ''">创建时间</label>
  82. <input th:type="${people!=null} ? 'text' : 'hidden'" class="form-control" placeholder="https://cdn.jxasp.com:9143/image/2020/3/12"
  83. th:value="${people!=null}?${#dates.format(people.createTime, 'yyyy-MM-dd hh:mm:ss')}" readonly="readonly" />
  84. </div>
  85. <button type="submit" class="btn btn-primary" th:text="${people!=null}?'修改':'添加'">添加</button>
  86. <p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
  87. </form>
  88. </div>
  89. </div>
  90. </main>
  91. </div>
  92. </div>
  93. <script type="text/javascript" src="asserts/js/jquery-3.2.1.slim.min.js" th:src="@{/webjars/jquery/3.3.1/jquery.js}"></script>
  94. <script type="text/javascript" src="asserts/js/popper.min.js" th:src="@{/webjars/popper.js/1.11.1/dist/popper.js}"></script>
  95. <script type="text/javascript" src="asserts/js/bootstrap.min.js" th:src="@{/webjars/bootstrap/4.0.0/js/bootstrap.js}"></script>
  96. <script type="text/javascript" src="#" th:src="@{/webjars/jquery/3.3.1/jquery.js}"></script>
  97. </body>
  98. </html>

首先要知道个知识点:

input隐藏使用type="hidden"

label隐藏使用display: none

这里可以通过后端是否发来了model,如果有model说明是编辑界面,如果没有model说明是添加!

然后进行对应的显示,隐藏即可:

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载