【问题标题】:How to pass semantic checkbox state (true/false) to e.target如何将语义复选框状态(真/假)传递给 e.target
【发布时间】:2021-11-11 06:51:03
【问题描述】:

目的: state.TNCstate.promos的值传递到 emailjs.sendForm。 如果选中该框,则提交的电子邮件应显示 'true',否则为 false。

我不知道我需要用谷歌搜索什么才能找到涉及 semanticemailjs 的解决方案...

这是我认为会为我做的代码。

value={this.state.promos}

value={this.state.TNC}

这些是他们应该作为值返回并被传递到 emailjs e.target

的状态
this.state = {
      TNC: false,
      promos: true,
    };

两个复选框的代码,(我已经尝试过使用和不使用 value={this.state.TNC}

<Form.Group widths="equal">
          <Form.Field>
            <Checkbox
              label="I agree to the Terms and Conditions"
              required
              control={Checkbox}
              data="TNC"
              onChange={this.onToggle}
              value={this.state.TNC}
            />
          </Form.Field>
          <Form.Field>
            <Checkbox
              label="Send me occasional updates and promotions"
              defaultChecked
              control={Checkbox}
              data="promos"
              value={this.state.promos}
              onChange={this.onTogglePromos}
            />
          </Form.Field>
        </Form.Group>

这里是完整代码,您可以在https://test.ghostrez.net找到表单的半功能版本

import emailjs from "emailjs-com";
import { Component } from "react";
import { Button, Checkbox, Form, Input, TextArea } from "semantic-ui-react";

export default class ContactUs extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      TNC: false,
      promos: true,
    };
  }
  onToggle = () => {
    const TNC = !this.state.TNC;
    this.setState({ TNC });
  };
  onTogglePromos = () => {
    const promos = !this.state.promos;
    this.setState({ promos });
  };
  sendEmail = (e, props) => {
    e.preventDefault();
    if (this.state.TNC !== false) {
      return emailjs
        .sendForm("tester", "testiTemp", e.target, "user_EPYkKnHwiytfdolol565ljQTCbJkzO7YD")
        .then(
          (result) => {
            alert("Email sent successfully!");
            e.target.reset();
          },
          (error) => {
            alert("Email send failed... :( I had one job...");
          }
        );
    } else {
      return alert(
        "You need to accept the Terms and Conditions before proceeding."
      );
    }
  };

  render() {
    return (
      <Form onSubmit={this.sendEmail}>
        <Form.Group widths="equal">
          <Form.Field
            id="firstName"
            control={Input}
            label="First name"
            name="firstName"
            placeholder="First name"
            required
          />
          <Form.Field
            id="lastName"
            name="lastName"
            control={Input}
            label="Last name"
            placeholder="Last name"
          />
        </Form.Group>
        <Form.Group widths="equal">
          <Form.Field
            id="Email"
            control={Input}
            label="Email"
            name="email"
            placeholder="joe@schmoe.com"
            required
          />
          <Form.Field
            id="Phone"
            control={Input}
            label="Phone"
            name="phone"
            placeholder="0469 420 689"
            required
          />
        </Form.Group>
        <Form.Field
          id="Message"
          control={TextArea}
          label="Message"
          name="message"
          placeholder="Message"
          required
        />
        <Form.Group widths="equal">
          <Form.Field>
            <Checkbox
              label="I agree to the Terms and Conditions"
              required
              control={Checkbox}
              data="TNC"
              onChange={this.onToggle}
              value={this.state.TNC}
            />
          </Form.Field>
          <Form.Field>
            <Checkbox
              label="Send me occasional updates and promotions"
              defaultChecked
              control={Checkbox}
              data="promos"
              value={this.state.promos}
              onChange={this.onTogglePromos}
            />
          </Form.Field>
        </Form.Group>
        <Form.Field
          id="Submit"
          control={Button}
          type="submit"
          value="submit"
          positive
          content="Submit"
        />
      </Form>
    );
  }
}

感谢您的帮助,非常感谢。

【问题讨论】:

    标签: javascript reactjs email semantic-ui


    【解决方案1】:

    这是对我有用的解决方案,尽管它仅在 true 时显示(选中复选框) 我已将id 值和value&checked 添加为(this.state.*state*)

    我的解决方法如下

    <Form.Group widths="equal">
              <Form.Field>
                <Checkbox
                id="Accept_TnCs"
                  label="I agree to the Terms and Conditions"
                  required
                  control={Checkbox}
                  name="TNC"
                  value={this.state.TNC}
                  checked={this.state.TNC}
                  onChange={this.onToggle}
                />
              </Form.Field>
              <Form.Field>
                <Checkbox
                id="Accept_Promos"
                  label="Send me occasional updates and promotions"
                  defaultChecked
                  control={Checkbox}
                  name="promos"
                  value={this.state.promos}
                  checked={this.state.promos}
                  onChange={this.onTogglePromos}
                />
              </Form.Field>
            </Form.Group>
    

    【讨论】:

      【解决方案2】:

      为了检查,属性不是value而是checked,试试这个:

              <Checkbox
                label="I agree to the Terms and Conditions"
                required
                control={Checkbox}
                data="TNC"
                onChange={this.onToggle}
                checked={this.state.TNC}
              />
      

      【讨论】:

      • 仍然没有传输值 :( 我尝试将 data 更改为 name 仍然没有
      猜你喜欢
      • 2012-06-01
      • 1970-01-01
      • 2019-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多