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

<script>
  export default {
    data() {
      return {
H
hdx 已提交
65
        nickname: '',
66
        gender: '0',
H
hdx 已提交
67
        age: 18,
H
hdx 已提交
68
        loves: ['0'],
H
hdx 已提交
69
        switch: true,
W
wanganxp 已提交
70
        comment:'',
71 72 73 74
        formData: {} as UTSJSONObject,
        // 仅测试
        testVerifySubmit: false,
        testVerifyReset: false,
75 76 77 78 79 80 81 82
      }
    },
    computed: {
      formDataText() : string {
        return JSON.stringify(this.formData)
      }
    },
    methods: {
H
hdx 已提交
83
      onFormSubmit: function (e : UniFormSubmitEvent) {
84
        this.formData = e.detail.value
85 86 87

        // 仅测试
        this.testVerifySubmit = (e.type == 'submit' && (e.target?.tagName ?? '') == "FORM")
88
      },
89
      onFormReset: function (e : UniFormResetEvent) {
90
        this.formData = {}
91 92 93

        // 仅测试
        this.testVerifyReset = (e.type == 'reset' && (e.target?.tagName ?? '') == "FORM")
94 95 96 97 98 99
      }
    }
  }
</script>

<style>
100
  .scroll-view {
101
    flex: 1;
102 103 104
  }

  .page {
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    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 已提交
129
  .btn-submit {
130 131 132
    margin-right: 5px;
  }

H
hdx 已提交
133
  .btn-reset {
134 135 136 137 138 139 140 141 142
    margin-left: 5px;
  }

  .result {
    margin-top: 30px;
  }

  .textarea {
    margin-top: 5px;
143
    padding: 5px;
144 145 146
    background-color: #fff;
  }
</style>