【问题标题】:Replacing an element value in an array using const and let in react使用 const 替换数组中的元素值并让反应
【发布时间】:2020-08-17 07:08:16
【问题描述】:

我有 10 个单选按钮,我只能选择其中一个。选择单选按钮时,布尔值相对于其 id 值从 false 更改为 true。

如果用户更改了选定的值,那么布尔数组的特定索引值必须更改为 true,通过将先前选定的值更改为 false,同样继续......

如何在反应中做到这一点?

 let questions;

class App extends Component {
  constructor(props) {
    super(props);
    console.log(this.props);
    this.state = {
      btnDisabled: true,
      questions: [],
    };
    this.changeRadioHandler = this.changeRadioHandler.bind(this);
    this.submitHandler = this.submitHandler.bind(this);
  }

  changeRadioHandler = (event) => {
    const qn = event.target.name;    
    let text = this.props.data.matrix.options;
    let userAnswer = [];
    for (let j = 0; j < text.length; j++) {
      userAnswer.push(false);      
    }
    const options = text.map((t, index) => ({
      text: t.text,
      userAnswer: userAnswer[index],
    }));    
    console.log(options);
    const question = {
      id: event.target.value,
      qn,
      options,
    };
    if (this.state.questions.some((question) => question.qn === qn)) {
      questions = [
        ...this.state.questions.filter((question) => question.qn !== qn),
        question,
      ];
      console.log("if loop", questions);
    } else {
      questions = [...this.state.questions, question];
    }
    this.setState({ questions });
    console.log(questions);
    if (questions.length === 10) {
      this.setState({
        btnDisabled: false,
      });
    }
  };

  submitHandler = () => {    
    console.log("btn clkd", questions);
  };

  render() {
    return (
      <div class="matrix-bd">
        {this.props.data.header_text && (
          <div class="header-qn">
            <h5>{this.props.data.header_text} </h5>
          </div>
        )}
        {this.props.data.matrix && (
          <div class="grid">
            {this.props.data.matrix.option_questions.map((questions, j) => {
              return (
                <div class="rows" key={j}>
                  <div class="cell main">{questions.text}</div>
                  {this.props.data.matrix.options.map((element, i) => {
                    return (
                      <div class="cell" key={i}>
                        <input
                          type="radio"
                          id={"rad" + j + i}
                          name={questions.text}
                          value={element.text}
                          onChange={this.changeRadioHandler}
                        ></input>
                        <label htmlFor={"rad" + j + i}>{element.text}</label>
                      </div>
                    );
                  })}
                </div>
              );
            })}
          </div>
        )}
        <div class="buttonsubmit text-right">
          <button
            type="button"
            id="QstnSubmit"
            name="QstnSubmit"
            class="btn btn-primary"
            disabled={this.state.btnDisabled}
            onClick={this.submitHandler}
          >
            {this.props.labels.LBLSUBMIT}
          </button>
        </div>
      </div>
    );
  }
}

export default App;

我的对象数组是这样的......

options: Array(10)
0: {text: "1", userAnswer: false}
1: {text: "2", userAnswer: false}
2: {text: "3", userAnswer: false}
3: {text: "4", userAnswer: false}
4: {text: "5", userAnswer: false}
5: {text: "6", userAnswer: false}
6: {text: "7", userAnswer: false}
7: {text: "8", userAnswer: false}
8: {text: "9", userAnswer: false}
9: {text: "10", userAnswer: false}
length: 10

【问题讨论】:

  • 运行代码时您期望发生什么?您只是将布尔数组中的每个索引都设置为 false。
  • 您能否更新并包含Minimal, Complete, and Reproducible 代码示例。
  • 我已经添加了完整的代码
  • 也许您可以尝试解释一下这段代码在做什么,以及您认为它应该做什么。照原样,无线电输入值是从 props.data.matrix.option 数组中设置的,因此如果要切换选定的无线电输入,则需要更新传入的道具。您能否提供 more 完整代码例如,也许是一个 running 代码和框来重现问题,并提供一组重现步骤?

标签: javascript arrays reactjs object radio-button


【解决方案1】:
changeRadioHandler = (event) => {
    const qn = event.target.name;
    const id = event.target.value;
    let text = this.props.data.matrix.options;
    let userAnswer = [];
    for (let j = 0; j < text.length; j++) {
      userAnswer.push(false);
    }
    const option = text.map((t, index) => ({
      text: t.text,
      userAnswer: userAnswer[index],
    }));
    const elIndex = option.findIndex((element, i) => element.text === id);
    let options = [...option];
    options[elIndex] = {
      ...options[elIndex],
      userAnswer: true,
    };
    const question = {
      id: event.target.value,
      qn,
      options,
    };
    if (this.state.questions.some((question) => question.qn === qn)) {
      questions = [
        ...this.state.questions.filter((question) => question.qn !== qn),
        question,
      ];
    } else {
      questions = [...this.state.questions, question];
    }
    this.setState({ questions });
    console.log(questions);
    if (questions.length === 10) {
      this.setState({
        btnDisabled: false,
      });
    }
  };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-14
    • 2016-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多