form.uvue 2.9 KB
Newer Older
1 2 3 4 5 6 7
<template>
  <!-- #ifdef APP -->
  <scroll-view class="page">
  <!-- #endif -->
    <form @submit="onFormSubmit" @reset="onFormReset">
      <view class="uni-form-item">
        <view class="title">姓名</view>
W
wanganxp 已提交
8
        <input class="uni-input" name="nickname" :value="nickname" placeholder="请输入姓名" />
9 10 11 12 13
      </view>
      <view class="uni-form-item">
        <view class="title">性别</view>
        <radio-group name="gender" class="flex-row">
          <view class="group-item">
雪洛's avatar
雪洛 已提交
14
            <radio :value="'0'" :checked="gender=='0'" /><text>男</text>
15 16
          </view>
          <view class="group-item">
雪洛's avatar
雪洛 已提交
17
            <radio :value="'1'" :checked="gender=='1'" /><text>女</text>
18 19 20 21 22 23 24
          </view>
        </radio-group>
      </view>
      <view class="uni-form-item">
        <view class="title">爱好</view>
        <checkbox-group name="loves" class="flex-row">
          <view class="group-item">
H
hdx 已提交
25
            <checkbox value="0" :checked="loves.indexOf('0')>-1" /><text>读书</text>
26 27
          </view>
          <view class="group-item">
H
hdx 已提交
28
            <checkbox value="1" :checked="loves.indexOf('1')>-1" /><text>写字</text>
29 30 31 32 33
          </view>
        </checkbox-group>
      </view>
      <view class="uni-form-item">
        <view class="title">年龄</view>
雪洛's avatar
雪洛 已提交
34
        <slider name="age" :value="age + ''" :show-value="true"></slider>
35 36 37 38
      </view>
      <view class="uni-form-item">
        <view class="title">保留选项</view>
        <view>
H
hdx 已提交
39
          <switch name="switch" :checked="switch" />
40 41 42
        </view>
      </view>
      <view class="uni-btn-v flex-row">
H
hdx 已提交
43 44
        <button class="btn btn-submit" form-type="submit" type="primary">Submit</button>
        <button class="btn btn-reset" type="default" form-type="reset">Reset</button>
45 46 47 48 49 50 51 52 53 54 55 56 57
      </view>
    </form>
    <view class="result">提交的表单数据</view>
    <textarea class="textarea" :value="formDataText"></textarea>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script>
  export default {
    data() {
      return {
H
hdx 已提交
58
        nickname: '',
59
        gender: '0',
H
hdx 已提交
60
        age: 18,
H
hdx 已提交
61
        loves: ['0'],
H
hdx 已提交
62
        switch: true,
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
        formData: {} as UTSJSONObject
      }
    },
    computed: {
      formDataText() : string {
        return JSON.stringify(this.formData)
      }
    },
    methods: {
      onFormSubmit: function (e : FormSubmitEvent) {
        this.formData = e.detail.value
      },
      onFormReset: function (_ : FormResetEvent) {
        this.formData = {}
      }
    }
  }
</script>

<style>
  .page {
    flex: 1;
    padding: 15px;
  }

  .flex-row {
    flex-direction: row;
  }

  .uni-form-item {
    padding: 15px 0;
  }

  .title {
    margin-bottom: 10px;
  }

  .group-item {
    flex-direction: row;
    margin-right: 20px;
  }

  .btn {
    flex: 1;
  }

H
hdx 已提交
109
  .btn-submit {
110 111 112
    margin-right: 5px;
  }

H
hdx 已提交
113
  .btn-reset {
114 115 116 117 118 119 120 121 122 123 124 125
    margin-left: 5px;
  }

  .result {
    margin-top: 30px;
  }

  .textarea {
    margin-top: 5px;
    background-color: #fff;
  }
</style>