微信小程序——调查问卷案例

绝地灬酷狼 2022-11-14 14:37 352阅读 0赞

文章目录

    • 案例链接
    • 页面效果
    • 页面设计
    • 页面样式
    • 页面逻辑
    • 后端代码(node.js)

案例链接








https://download.csdn.net/download/weixin_45525272/16242305

页面效果

在这里插入图片描述

页面设计

  1. <view class="container">
  2. <form bindsubmit="submit">
  3. <view>
  4. <text>姓名:</text>
  5. <input name="name" value="{ {name}}" />
  6. </view>
  7. <view>
  8. <text>性别:</text>
  9. <radio-group name="gender">
  10. <label wx:for="{ {gender}}" wx:key="value">
  11. <radio value="{ {item.value}}" checked="{ {item.checked}}" /> {
  12. {item.name}}
  13. </label>
  14. </radio-group>
  15. </view>
  16. <view>
  17. <text>专业技能:</text>
  18. <checkbox-group name="skills">
  19. <label wx:for="{ {skills}}" wx:key="value">
  20. <checkbox value="{ {item.value}}" checked="{ {item.checked}}" /> {
  21. {item.name}}
  22. </label>
  23. </checkbox-group>
  24. </view>
  25. <view>
  26. <text>您的意见:</text>
  27. <textarea name="opinion" value="{ {opinion}}" />
  28. </view>
  29. <button form-type="submit">提交</button>
  30. </form>
  31. </view>

页面样式

  1. .container {
  2. margin: 50rpx;
  3. }
  4. view {
  5. margin-bottom: 30rpx;
  6. }
  7. input {
  8. width: 600rpx;
  9. margin-top: 10rpx;
  10. border-bottom: 2rpx solid #ccc;
  11. }
  12. label {
  13. display: block;
  14. margin: 8rpx;
  15. }
  16. textarea {
  17. width: 600rpx;
  18. height: 100rpx;
  19. margin-top: 10rpx;
  20. border: 2rpx solid #eee;
  21. }

页面逻辑

前端发送请求到后端,后端传回 json 数组赋值给 data

  1. // pages/index/index.js
  2. Page({
  3. /** * 页面的初始数据 */
  4. data: {
  5. },
  6. /** * 生命周期函数--监听页面加载 */
  7. onLoad: function(options) {
  8. wx.request({
  9. url: 'http://127.0.0.1:3000/',
  10. success: res => {
  11. console.log(res.data)
  12. this.setData(res.data)
  13. }
  14. })
  15. },
  16. submit: function(e) {
  17. wx.request({
  18. method: 'post',
  19. url: 'http://127.0.0.1:3000/',
  20. data: e.detail.value,
  21. success: function(res) {
  22. console.log(res)
  23. }
  24. })
  25. }
  26. })

后端代码(node.js)

直接 node index.js 启动即可 (node.js不会的小伙伴可以参考我的node.js专栏 https://blog.csdn.net/weixin_45525272/category_10080631.html?spm=1001.2014.3001.5482)

  1. const express = require('express')
  2. const bodyParser = require('body-parser')
  3. const app = express()
  4. app.use(bodyParser.json())
  5. // 处理POST请求
  6. app.post('/', (req, res) => {
  7. console.log(req.body)
  8. res.json(req.body)
  9. })
  10. var data = {
  11. name: '张三',
  12. gender: [
  13. { name: '男', value: '0', checked: true },
  14. { name: '女', value: '1', checked: false }
  15. ],
  16. skills: [
  17. { name: 'HTML', value: 'html', checked: true },
  18. { name: 'CSS', value: 'css', checked: true },
  19. { name: 'JavaScript', value: 'js', checked: true },
  20. { name: 'Photoshop', value: 'ps', checked: false },
  21. ],
  22. opinion: '测试'
  23. }
  24. // 处理GET请求
  25. app.get('/', (req, res) => {
  26. res.json(data)
  27. })
  28. // 监听3000端口
  29. app.listen(3000, () => {
  30. console.log('server running at http://127.0.0.1:3000')
  31. })

发表评论

表情:
评论列表 (有 0 条评论,352人围观)

还没有评论,来说两句吧...

相关阅读

    相关 HDU6344 调查问卷

    状态压缩 + 模拟 把AB串压缩成二进制,A用1表示,B用0表示。 枚举所有问题的子集,选中的问题用1表示,其余的用0表示。对于每个子集,我们去和所有问题按位与,这样对