微信小程序常用表单组件的归纳与整理

why 1244 2024-06-19

本篇文章给大家带来了关于微信小程序的相关知识,其中主要介绍了一些常用表单组件,包括了button、checkbox、input、label等等相关问题,下面一起来看一下,希望对大家有帮助。

归纳整理微信小程序常用表单组件


1、常用表单组件

1.1 button

  

image.png

代码示例:

1

2

3

4

5

6

7

8

9

10

11

12

<view>

  <view>7.button小案例</view>

  <view>(1)迷你按钮</view>

  <button>主要按钮</button>

  <button>次要按钮</button>

  <button>警告按钮</button>

  <view>(2)按钮状态</view>

  <button>普通按钮</button>

  <button>警用按钮</button>

  <button>加载按钮</button>

  <view>(3)增加按钮事件</view>

  <button>点我获取用户信息</button></view>

image.png

1.2 checkbox

  为复选框组件,常用于在表单中进行多项数据的选择。复选框的为父控件,其内部嵌套若干个子控件。

  属性如下:

image.png

  组件的属性如下:

image.png

代码示例:

checkbox.wxml

1

2

3

4

5

6

7

8

<view>

  <view>8.checkbox小案例</view>

  <view>利用for循环批量生成</view>

  <checkbox-group>

    <label>

      <checkbox></checkbox>{{item.value}}

    </label>

  </checkbox-group></view>

checkbox.js

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Page({

  data: {

    items: [

      { name: "tiger", value: "老虎" },

      { name: "elephant", value: "大象" },

      { name: "lion", value: "狮子", checked: "true" },

      { name: "penguin", value: "企鹅" },

      { name: "elk", value: "麋鹿" },

      { name: "swan", value: "天鹅" },

    ]

  },

  checkboxChange:function(e) {

    console.log("checkbox发生change事件,携带value值为:", e.detail.value)

  }})

image.png

1.3 input

  为输入框组件,常用于文本(如姓名、年龄等信息)的输入。属性表如下:

image.png

1

2

3

4

5

6

7

8

9

10

<view>

  <view>9.input小案例</view>

  <view>(1)文字输入框</view>

  <input>

  <view>(2)密码输入框</view>

  <input>

  <view>(3)禁用输入框</view>

  <input>

  <view>(4)为输入框增加事件监听</view>

  <input></view>

image.png

1.4 label

  

image.png

wxml

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<view>

  <view>10.lable小案例</view>

  <view>(1)利用for属性</view>

  <checkbox-group>

    <checkbox></checkbox>

    <label>老虎</label>

    <checkbox></checkbox>

    <label>大象</label>

    <checkbox></checkbox>

    <label>狮子</label>

  </checkbox-group>

  <view>(2)label包裹组件</view>

  <checkbox-group>

    <label>

      <checkbox></checkbox>老虎    </label>

    <label>

      <checkbox></checkbox>大象    </label>

    <label>

      <checkbox></checkbox>狮子    </label>

  </checkbox-group></view>

1.5 form

  

为表单控件组件,用于提交表单组件中的内容。
控件组件内部可以嵌套多种组件。

  组件属性如下:

image.png

form.wxml

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<view>

  <view>11.form小案例</view>

  <view>模拟注册功能</view>

  <form>

    <text>用户名:</text>

    <input>

    <text>密码:</text>

    <input>

    <text>手机号:</text>

    <input>

    <text>验证码:</text>

    <input>

    <button>注册</button>

    <button>重置</button>

  </form></view>

form.js

1

2

3

4

5

6

7

8

Page({

  onSubmit(e) {

    console.log("form发生了submit事件,携带数据为:")

    console.log(e.detail.value)

  },

  onReset() {

    console.log("form发生了reset事件,表单已被重置")

  }})

  输入测试数据后点击注册按钮触发表单提交事件。

image.png

1.6 radio

  为单选框组件,往往需配合组件来使用,标签嵌套在当中。

  组件属性如下:

image.png

  组件属性如下:

image.png

radio.wxml

1

2

3

4

5

6

7

<view>

  <view>14.radio小案例</view>

  <view>利用for循环批量生成</view>

  <radio-group>

    <block>

      <radio></radio>{{item.value}}    </block>

  </radio-group></view>

radio.js

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Page({

  data: {

    radioItems: [

      { name: 'tiger', value: '老虎' },

      { name: 'elephant', value: '大象' },

      { name: 'lion', value: '狮子', checked: 'true' },

      { name: 'penguin', value: '企鹅' },

      { name: 'elk', value: '麋鹿' },

      { name: 'swan', value: '天鹅' },

    ]

  },

  radioChange:function(e) {

    console.log("radio发生change事件,携带value值为:", e.detail.value)

  }})

image.png

1.7 slider

  为滑动选择器,用于可视化地动态改变某变量地取值。属性表如下:

image.png

slider.wxml

1

2

3

4

5

6

7

8

9

10

<view>

  <view>15.slider小案例</view>

  <view>(1)滑动条右侧显示当前进度值</view>

  <slider></slider>

  <view>(2)自定义滑动条颜色与滑块样式</view>

  <slider></slider>

  <view>(3)禁用滑动条</view>

  <slider></slider>

  <view>(4)增加滑动条监听事件</view>

  <slider></slider></view>

image.png

1.8 switch

  为开关选择器,常用于表单上地开关功能,属性表如下所示。

image.png

switch.wxml

1

2

3

4

5

<view>

  <view>16.switch小案例</view>

  <view>增加switch事件监听</view>

  <switch></switch>

  <switch></switch></view>

image.png

1.9 textarea

  

2、实训小案例–问卷调查

survey.wxml

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

<view>

  <form>

    <view>1.你现在大几?</view>

    <radio-group>

      <radio></radio>大一      <radio></radio>大二      <radio></radio>大三      <radio></radio>大四    </radio-group>

 

    <view>2.你使用手机最大的用途是什么?</view>

    <checkbox-group>

      <label><checkbox></checkbox>社交</label>

      <label>

        <checkbox></checkbox>网购</label>

      <label>

        <checkbox></checkbox>学习</label><label>

        <checkbox></checkbox>其他</label>

 

    </checkbox-group>

    <view>3.平时每天使用手机多少小时?</view>

    <slider></slider>

 

     <view>4.你之前有使用过微信小程序吗?</view>

    <radio-group>

      <radio></radio>无      <radio></radio>有    </radio-group>

 

    <view>5.谈谈你对微信小程序未来发展的看法</view>

    <textarea></textarea>

    <button>提交</button>

    <button>重置</button>

  </form></view>

survey.js

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

Page({

  universityChange: function (e) {

    console.log("你选择的现在大几:", e.detail.value)

  },

 

  mobilChange: function (e) {

    console.log("你选择使用手机的最大用途是:", e.detail.value)

  },

 

 

  timechange: function (e) {

    console.log("你选择的每天使用手机的时间是:", e.detail.value + "小时")

  },

 

  programChange: function (e) {

    console.log("你选择的是否使用过微信小程序:", e.detail.value)

  },

  

  

  onSubmit(e) {

    console.log("你输入的对小程序发展前途的看法是:"+e.detail.value.textarea)

 

  },

  onReset() {

    console.log("表单已被重置")

  }})

image.png


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:附近小程序怎么开发全攻略
下一篇:微信小程序中自定义组件方法的简要分析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~