如何将语义复选框状态 (true/false) 传递给 e.target

How to pass semantic checkbox state (true/false) to e.target

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

我不知道我需要什么 google 来找到涉及 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>
    );
  }
}

谢谢你的帮助,我真的很感激。

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

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

这是对我有用的解决方案,尽管它仅在 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>