javascript - 在 React/Typescript 中处理多个开关控件的函数

标签 javascript reactjs typescript ecmascript-2016

我想做的是发送多个开关控制的发布请求,但是我认为它们必须是一种更简单的发送请求的方法,而不是为每个开关输入单独编写请求。我如何整合或编写一个可以干净地完成此操作的函数?使用 typescript / react 。问题更新如下:

更新:我想要做的是将开关输入控件的状态作为切换发送。 IE。当开关控件设置为yes/no时,则向服务器发送post请求

    interface IState {
  checkedA?: boolean;
  checkedB?: boolean;
}

  public render() {
const {} = this.state;

 <div className={classes.switchContainer}>
                <FormGroup row>
                    <FormControlLabel
                      control={
                        <Switch
                          checked={this.state.checkedA}
                          onChange={this.handleChange}
                          value="checkedA"
                        >Toggle</Switch>
                        }label="YES"
                        />
                  <Typography color="secondary" variant="body1" className={classes.toggleQuestions}>Question 1</Typography>
                </FormGroup>
              </div>

     <div className={classes.switchContainer}>
                    <FormGroup row>
                        <FormControlLabel
                          control={
                            <Switch
                              checked={this.state.checkedB}
                              onChange={this.handleChange}
                              value="checkedB"
                            >Toggle</Switch>
                            }label="YES"
                            />
                      <Typography color="secondary" variant="body1" className={classes.toggleQuestions}>Question 2</Typography>
                    </FormGroup>
                  </div>
)}

<Button
 onClick={this.submitRecommendation}>
Send Request</Button



 await axios.post(
      `/test/testpost`,
      {  
       post request from click of button would go in here
      } catch (err) {
}
);

最佳答案

以下是如何在表单提交时以编程方式访问输入的示例:

private handleSubmit = (e: React.FormEvent): void => {
  e.preventDefault();
  // We want the form as a starting point to traverse the DOM
  const form = e.target;
  // It seems like we can be relatively sure that all the information 
  // we want is contained in the form's inputs, so we'll select them all
  const inputs = form.querySelectorAll('input');
  // With the NodeList inputs (a DOM data type), we can use .forEach 
  // to check out all the nodes by their type
  inputs.forEach(input => {
    // Accessing the name and value will give you all
    // the information you need to build up a POST request
    switch(input.type) {
      case 'checkbox':
        console.log('The checkbox is called', input.name, 'and its checked state is', input.checked);
        break;
      case 'text':
        console.log('The text input is called', input.name, 'and it says', input.value);
        break;
    }
  });
}

下面是一个示例,说明如何在 .forEach 循环中构建请求对象,构建一个要在请求中发送的对象。如果您确保为每个项目使用正确的 name 属性,您可以很容易地做到这一点。

// in the above method
const request = {};
inputs.forEach(input => {
  // Somehow map the input name and value to your data model you're updating
  request[input.name] = input.value;
});
await axios.post(/* use request object here */);

希望这足以帮助您入门,但如果您想讨论任何具体的实现细节,请随时发表评论! :D

关于javascript - 在 React/Typescript 中处理多个开关控件的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54172808/

相关文章:

javascript - 如何向客户端设置 CORS header

angular - 使用对象定义定义 FomGroup

javascript - 如何使用 javascript 检索 html 中所有突出显示的单词

javascript - 数组如何工作(Javascript)?

javascript - 如何在 jQuery 中监听 HTML5 视频停止播放的情况?

javascript - enzyme 、react-dom/test-tools 或 jsdom 中是否存在影响 clientHeight 的已知缺陷?

reactjs - 在 react Spring 动画完成时更改状态,

angular - 在 ngFor 循环中动态地向元素添加一个类

javascript - Angular 将 ViewChild 绑定(bind)到类中的属性

javascript - 切换折叠菜单始终打开