You need to enable JavaScript to run this app. React类组件表单集成下拉选择框提交数据到Google表格咨询
优惠活动
大模型
产品
解决方案
定价
更多

React类组件表单集成下拉选择框提交数据到Google表格咨询

实现步骤
  • 第一步:在组件的state中新增participation字段,用于存储下拉选择的应答值
  • 第二步:在App组件外部定义下拉选项常量PARTICIPATION
  • 第三步:在现有表单的两个输入框下方,新增下拉选择的表单字段,给select标签添加name="participation"属性,直接复用已有的changeHandler作为onChange事件处理函数,不需要额外编写新的状态更新逻辑
  • 第四步:遍历PARTICIPATION数组生成下拉的option节点即可

原有的提交逻辑不需要任何修改,提交时会自动把participation的值和其他字段一起发送到接口。

完整修改后代码
import React, { Component } from 'react'
import { Button, Form, Container, Header } from 'semantic-ui-react'
import './App.css';
import axios from 'axios';

// 新增下拉选项常量
const PARTICIPATION = [
  "Yes, I would like to participate in this activity.",
  "Sí, me gustaría participar en esta actividad."
];

export default class App extends Component {
  constructor(props) {
    super(props)
  
    this.state = {
       name: '',
       email: '',
       participation: '' // 新增选择字段存储
    }
  }

  changeHandler = (e) => {
    this.setState({[e.target.name] : e.target.value})
  }

  submitHandler = e => {
    e.preventDefault();
    console.log(this.state);

    axios.post('url', this.state)
    .then(response => {
      console.log(response);
    })
  }
  
  render() {
    // 新增participation解构
    const { name, email, participation } = this.state; 
    return (
      <Container fluid className="container">
        <Header as='h2'>Activity Sign-Up</Header>
        <Form className="form" onSubmit={this.submitHandler}>

          <Form.Field>
            <label>Name | Nombre</label>
            <input placeholder='Name / Nombre' type="text" name = "name" value = {name} onChange={this.changeHandler}/>
          </Form.Field>
          <Form.Field>
            <label>Email | Correo electrónico</label>
            <input placeholder='Email / Correo electrónico' type="email" name = "email" value = {email} onChange={this.changeHandler}/>
          </Form.Field>

          {/* 新增下拉选择字段 */}
          <Form.Field>
            <label>Participation | Participación*:</label>
            <select name="participation" value={participation} onChange={this.changeHandler}>
              <option value=""> </option>
              {PARTICIPATION.map((c) => (
                <option key={c} value={c}>{c}</option>
              ))}
            </select>
          </Form.Field>

          <Button color="blue" type='submit'>Submit | Enviar</Button>
        </Form>
      </Container>
    )
  }
}

内容的提问来源于stack exchange,提问作者newToThis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.15 17:54:00