抓取提交的React表单字段值

时间:2018-04-26 15:56:09

标签: reactjs superagent

给定React表单,我无法从所选单选按钮获取值,如果选择了other,则会出现文本框。我应该能够将字段传递到帖子的send(),但不知道如何抓住它们。

class CancelSurvey extends React.Component {
constructor (props) {
  super(props)
  this.state = {
    reasons: [],
    reason: {}
  }
  this.processData = this.processData.bind(this)
  this.handleSubmit = this.handleSubmit.bind(this)
  this.otherSelected = this.state.reason === "otheroption";
}

componentDidMount () {
  this.fetchContent(this.processData)
}

/**
 * Fetch reasons
 */

fetchContent (cb) {
  superagent
    .get('/api/user/survey')
    .then(cb)
}

/**
 * Set state after reasons have been fetched
 * @param data
 */

processData (data) {
  this.setState({
    reasons: data.body
  })
}

handleSubmit (e) {
  e.preventDefault()
  let reason = this.state.reason
  if (reason === 'otheroption') {
    reason = this.state.otherreason
  }
  console.log(reason)
  superagent
    .post('/api/user/survey')
    .send({
      optionId: this.state.reason.reason_id,
      optionText: this.state.reason.client_reason,
      otherReasonText: this.state.otherreason
    })
    .then(function (res) {
      console.log('Survey Sent!')
    })
}
  /**
   * render
   */
  render (props) {
    const content = this.props.config.contentStrings
    const reason = this.state.reasons.map((reason, i) => {
      return (
        <div className='fieldset__item' key={i}>
          <label>{reason.client_reason}</label>
          <input type='radio'
            id={reason.reason_id}
            value={reason.client_reason}
            name='reason'
            checked={this.state.reason.reason_id === reason.reason_id}
            onChange={() => this.setState({reason})} />
        </div>
      )
    })

    return (
      <div className='survey'>
        <h2 className='heading md'>{content.memberCancel.exitSurvey.heading}</h2>
        <p className='subpara'>{content.memberCancel.exitSurvey.subHeading}</p>
        <form id='exit-survey' onSubmit={this.handleSubmit}>
          <fieldset className='fieldset'>
            { reason }
            <label>Other reason not included above:</label>
            <input type='radio'
              id='otheroption'
              name='reason'
              value={this.state.reason.otherreason}
              onChange={() => this.setState({reason:{reason_id: 70, client_reason: 'other'}})} />
            <input className='valid'
              type='text'
              id='otheroption'
              name='othertext'
              placeholder={content.memberCancel.exitSurvey.reasonPlaceholder}
              onChange={(event) => this.setState({otherreason: event.target.value})} />
          </fieldset>
          <div className='footer-links'>
            <button className='btn btn--primary btn--lg' onClick={this.handleSubmit}>{content.memberCancel.exitSurvey.button}</button>
          </div>
        </form>
      </div>
    )
  }
}

export default CancelSurvey

1 个答案:

答案 0 :(得分:0)

您的变量不正确。我已将它们更新为我认为正确的内容。

handleSubmit (e) {
  e.preventDefault()
  superagent
    .post('/api/user/survey')
    .send({
      optionId: this.state.reason.reason_id,
      optionText: this.state.reason.client_reason,
      otherReasonText: this.state.reason.otherreason
    })
    .then(function (res) {
      console.log('Survey Sent!')
    })
    .catch(function (err) {
      console.log('Survey submission went wrong...')
    })
}
/**
 * render
 */
render (props) {
    const content = this.props.config.contentStrings
    const reason = this.state.reasons.map((reason, i) => {
        return (
            <div className='fieldset__item' key={i}>
                <label>{reason.client_reason}</label>
                <input 
                    type='radio'
                    id={reason.reason_id}
                    name='reason'
                    checked={this.state.reason.reason_id === reason.reason_id}
                    value={reason.client_reason}
                    onChange={() => this.setState({reason})} />
            </div>
        )
    })

    return (
        <div className='survey'>
            <h2 className='heading md'>{content.memberCancel.exitSurvey.heading}</h2>
            <p className='subpara'>{content.memberCancel.exitSurvey.subHeading}</p>
            <form id='exit-survey' onSubmit={this.handleSubmit}>
                <fieldset className='fieldset'>
                    { reason }
                    <label>Other reason not included above:</label>
                    <input type='radio'
                        id='otheroption'
                        name='otheroption'
                        value={this.state.reason.otherreason}
                        checked={this.state.reason.reason_id === 0}
                        onChange={() => this.setState({ reason: {reason_id: 0, client_reason: ""} })} />
                    <input className='valid'
                        type='text'
                        id='othertext'
                        name='othertext'
                        value={this.state.reason.otherreason}
                        placeholder={content.memberCancel.exitSurvey.reasonPlaceholder}
                        onChange={(event) => this.setState({ reason: {reason_id: 0, client_reason: "", otherreason: event.target.value} })} />
                </fieldset>
                <div className='footer-links'>
                    <button className='btn btn--primary btn--lg' onClick={this.handleSubmit}>{content.memberCancel.exitSurvey.button}</button>
                </div>
            </form>
        </div>
    );
}