<form bindsubmit="formSubmit" bindreset="formReset">
<view class="group">
<view class="title">用户名:</view>
<view class="cont">
<input type="text" name="username" placeholder="请输入用户名"/>
</view>
<view class="clear"></view>
</view>
<view class="group">
<view class="title">性别:</view>
<view class="cont">
<radio-group name="gender">
<label><radio value="1"/>男</label>
<label><radio value="0"/>女</label>
</radio-group>
</view>
<view class="clear"></view>
</view>
<view class="group">
<view class="title">food:</view>
<view class="cont">
<checkbox-group name="hobby">
<label><checkbox value="0"/>蛋糕</label>
<label><checkbox value="1"/>牛排</label>
<label><checkbox value="1"/>排骨头</label>
</checkbox-group>
</view>
<view class="clear"></view>
</view>
<view class="group">
<view class="title">同意:</view>
<view class="cont">
<switch name="isagree"/>
</view>
<view class="clear"></view>
</view>
<view class="group">
<button form-type="submit">提交表单</button>
<button form-type="reset">清空表单</button>
</view>
</form>
.clear{
clear: both;
}
.title{
float: left;
width: 100px;
text-align: right;
}
.cont{
float: left;
}
input{
border:1px solid gainsboro;
}
.group{
margin-top:20px;
}
// 提交表单函数
formSubmit:function(e){
console.log(e);
console.log("表单已经提交!");
console.log("用户名:"+e.detail.value.username);
console.log("性别:" + (e.detail.value.gender==1?"男":"女"));
},
// 清空表单函数
formReset:function(e){
console.log("表单已经清空!");
},
效果图:
注意:formSubmit为表单提交后执行的函数,e.detail.value中是表单提交上来的数据,这里要注意,存放数据的标签一定要有name属性值才能获取数据