微信小程序 weui 编写单选投票
时间: 2024-05-05 22:15:58 浏览: 150
微信投票小程序
4星 · 用户满意度95%
以下是一个简单的微信小程序 weui 实现单选投票的代码示例:
1. 在 app.json 文件中添加页面路径:
```
{
"pages": [
"pages/index/index",
"pages/vote/vote"
],
"window": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "WeChat",
"navigationBarTextStyle": "black"
}
}
```
2. 在 index 页面中添加一个跳转到投票页面的按钮:
```
<view class="container">
<button class="weui-btn" bindtap="goToVote">去投票</button>
</view>
```
3. 在 index 页面的 js 文件中添加 goToVote 函数:
```
Page({
goToVote: function() {
wx.navigateTo({
url: '/pages/vote/vote'
})
}
})
```
4. 在 vote 页面中添加一个 radio-group 和若干 radio 控件:
```
<view class="container">
<form bindsubmit="submitVote">
<view class="weui-cells weui-cells_radio">
<label class="weui-cell weui-check__label" for="option1">
<view class="weui-cell__bd">选项1</view>
<view class="weui-cell__ft">
<radio class="weui-check" name="options" value="option1" checked="true"></radio>
</view>
</label>
<label class="weui-cell weui-check__label" for="option2">
<view class="weui-cell__bd">选项2</view>
<view class="weui-cell__ft">
<radio class="weui-check" name="options" value="option2"></radio>
</view>
</label>
<label class="weui-cell weui-check__label" for="option3">
<view class="weui-cell__bd">选项3</view>
<view class="weui-cell__ft">
<radio class="weui-check" name="options" value="option3"></radio>
</view>
</label>
</view>
<button class="weui-btn weui-btn_primary" form-type="submit">投票</button>
</form>
</view>
```
5. 在 vote 页面的 js 文件中添加 submitVote 函数:
```
Page({
submitVote: function(e) {
var option = e.detail.value.options;
wx.showModal({
title: '投票结果',
content: '您选择了' + option,
showCancel: false
})
}
})
```
这样就完成了一个简单的单选投票的微信小程序 weui 实现。
阅读全文