Represents a form. It is used to submit the switch, input, checkbox, slider, radio, and picker entered by users in the component.
When a button component with form-type set to submit is tapped within a form, the values of the form components are submitted. Each form component must include a name attribute to serve as the key.
Since: 1.0.0
Events
| Name | Type | Mandatory | Description |
|---|---|---|---|
| bindsubmit | eventhandle | No | Handler for form submit events. |
| bindreset | eventhandle | No | Handler for form reset events. |
Example:
HXML file:
<form bindsubmit="formSubmit" bindreset="formReset">
<view class="page-section page-section-gap">
<view class="page-section-title">switch</view>
<switch name="switch" />
</view>
<view class="page-section page-section-gap">
<view class="page-section-title">radio</view>
<radio-group name="radio">
<label>
<radio value="radio1" />Option 1
</label>
<label>
<radio value="radio2" />Option 2
</label>
</radio-group>
</view>
<view class="page-section page-section-gap">
<view class="page-section-title">checkbox</view>
<checkbox-group name="checkbox">
<label>
<checkbox value="checkbox1" />Option 1
</label>
<label>
<checkbox value="checkbox2" />Option 2
</label>
</checkbox-group>
</view>
<view class="page-section page-section-gap">
<view class="page-section-title">slider</view>
<slider value="50" name="slider" show-value></slider>
</view>
<view class="page-section">
<view class="page-section-title">input</view>
<view class="ascf-cells">
<view class="ascf-cell ascf-cell_input">
<view class="ascf-cell__bd" style="margin: 30rpx 30rpx">
<input class="ascf-input" name="input" placeholder="This is an input box" />
</view>
</view>
</view>
</view>
<view class="btn-area">
<button style="margin: 30rpx 0" type="primary" formType="submit">
Submit
</button>
<button style="margin: 30rpx 0" formType="reset">Reset</button>
</view>
</form> JavaScript file:
Page({
data: {
formSubmitResult: '',
formResetResult: ''
},
formSubmit(event) {
// Example of event.detail.value: {"switch":false,"radio":"","checkbox":[],"slider":0,"input":""}
console.info('A submit event occurred in the form. Carried data: ', event.detail.value);
this.setData({
formSubmitResult: event.detail.value
});
},
formReset(event) {
// Example of event.detail: {"target":{"tagName":"","id":"","offsetLeft":0,"offsetTop":0,"dataset":{}}}.
console.info('A reset event occurred in the form. Carried data: ', event.detail);
this.setData({
formResetResult: event.detail
});
}
});