vue.js实现单选获取选中值

今天药忘吃喽~ 2021-12-01 12:16 521阅读 0赞
  1. <!DOCTYPE HTML>
  2. <html lang="zh-cn">
  3. <head>
  4. <meta http-equiv="Content-type" content="text/html; charset=UTF-8">
  5. <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
  6. <meta name="renderer" content="webkit">
  7. <link rel="stylesheet" href="css/style.css" type="text/css">
  8. <title>单选</title>
  9. <script src="vue.js" type="text/javascript"></script>
  10. <style>
  11. ul, li {
  12. list-style-type: none;
  13. }
  14. * {
  15. margin: 0;
  16. padding: 0;
  17. }
  18. .border-1px {
  19. position: relative;
  20. }
  21. .border-1px:after {
  22. display: block;
  23. position: absolute;
  24. left: 0;
  25. bottom: 0;
  26. width: 100%;
  27. border-top: 1px solid rgba(7, 17, 27, .1);
  28. content: ' ';
  29. }
  30. @media (-webkit-min-device-pixel-ratio: 1.5),(min-device-pixel-ratio: 1.5) {
  31. .border-1px::after {
  32. -webkit-transform: scaleY(0.7);
  33. transform: scaleY(0.7);
  34. }
  35. }
  36. @media (-webkit-min-device-pixel-ratio: 2),(min-device-pixel-ratio: 2) {
  37. .border-1px ::after {
  38. -webkit-transform: scaleY(0.5);
  39. transform: scaleY(0.5);
  40. }
  41. }
  42. #example {
  43. margin: 20px;
  44. }
  45. h3 {
  46. font-size: 26px;
  47. margin-left: 20px;
  48. height: 60px;
  49. }
  50. .self-radio {
  51. display: none;
  52. }
  53. .self-radio + label {
  54. -webkit-appearance: none;
  55. background-color: #fff;
  56. border: 1px solid #aaa;
  57. border-radius: 50px;
  58. display: inline-block;
  59. position: relative;
  60. width: 30px;
  61. height: 30px;
  62. box-sizing: border-box;
  63. }
  64. .self-radio:checked + label {
  65. border: 1px #47d9bf solid;
  66. }
  67. .self-radio:checked + label:after {
  68. position: absolute;
  69. top: 9px;
  70. left: 9px;
  71. content: ' ';
  72. width: 10px;
  73. height: 10px;
  74. border-radius: 50px;
  75. background: #47d9bf;
  76. box-shadow: 0px 0px 5px 0px #47d9bf;
  77. }
  78. .check-area {
  79. display: inline-block;
  80. width: 400px;
  81. padding: 12px 20px;
  82. border: 1px solid #aaa;
  83. border-top-left-radius: 4px;
  84. border-top-right-radius: 4px;
  85. }
  86. li {
  87. height: 60px;
  88. }
  89. li .self-radio + label {
  90. vertical-align: middle;
  91. }
  92. li span {
  93. margin-left: 20px;
  94. display: inline-block;
  95. line-height: 60px;
  96. font-size: 22px;
  97. }
  98. p {
  99. height: 60px;
  100. line-height: 60px;
  101. margin-left: 20px;
  102. }
  103. p span {
  104. color: #f00;
  105. }
  106. .btn {
  107. margin: 20px auto;
  108. width: 100%;
  109. text-align: center;
  110. }
  111. .btn button {
  112. width: 120px;
  113. height: 40px;
  114. line-height: 30px;
  115. font-size: 16px;
  116. color: #fff;
  117. background: #47d9bf;
  118. border: 1px #23d5b6 solid;
  119. border-radius: 6px;
  120. text-align: center;
  121. outline: none;
  122. }
  123. .btn button:hover {
  124. background: #23d5b6;
  125. }
  126. </style>
  127. </head>
  128. <body>
  129. <div id="example">
  130. <h3>单选按钮</h3>
  131. <div class="check-area" v-show="items.length!=0">
  132. <ul>
  133. <li class="border-1px" v-for="(item,index) in items">
  134. <input class="self-radio" type="radio"
  135. :id="'radio-'+item.id"
  136. :data-id="'food-'+item.id" name="radio"
  137. :checked="index==0"
  138. :value="item.value"
  139. v-model="checkValue">
  140. <label :for="'radio-'+item.id" @click="setCheckValue(item)"></label>
  141. <span>{
  142. {item.value}}</span>
  143. </li>
  144. </ul>
  145. <p>您选择了:<span>{
  146. {checkValue}}</span></p>
  147. <div class="btn">
  148. <button @click="showCheck(checkId)">按钮</button>
  149. <span>{
  150. {checkId}}</span>
  151. </div>
  152. </div>
  153. </div>
  154. <script>
  155. var itemData = [
  156. {id: '20170811001', value: '香蕉'},
  157. {id: '20170811002', value: '苹果'},
  158. {id: '20170811003',value: '梨子'},
  159. {id: '20170811004', value: '葡萄'}
  160. ]
  161. //itemData = [];
  162. var vm = new Vue({
  163. el: '#example',
  164. data: {
  165. items: '',
  166. checkValue: '',
  167. checkId: ''
  168. },
  169. methods: {
  170. init: function () {
  171. },
  172. initData: function () {
  173. var self = this;
  174. self.items = itemData;
  175. if (itemData.length != 0) {
  176. self.checkValue = self.items[0].value;
  177. self.checkId = 'food-' + self.items[0].id
  178. }
  179. },
  180. setCheckValue: function (item) {
  181. this.checkId = 'food-' + item.id;
  182. }
  183. ,
  184. showCheck: function () {
  185. console.log(this.checkId)
  186. }
  187. },
  188. mounted: function () {
  189. this.initData();
  190. }
  191. })
  192. </script>
  193. </body>
  194. </html>

发表评论

表情:
评论列表 (有 0 条评论,521人围观)

还没有评论,来说两句吧...

相关阅读