简体   繁体   English

如何根据当前状态值检查/激活单选按钮

[英]How to make radio button checked/active depending on current state value

so I've got a header that cycles through 4 images every 5 seconds.所以我有一个标题,每 5 秒循环 4 个图像。 The user can also cycle through these images themselves using 5 radio buttons.用户还可以使用 5 个单选按钮自己循环浏览这些图像。

The styling for the active/checked radio button is applied when the user clicks it themselves, however, if the timer switches the image the active radio button remains unchanged.活动/选中单选按钮的样式在用户自己单击时应用,但是,如果计时器切换图像,活动单选按钮保持不变。

I know this is the expected behaviour based on my code below, I'm just wondering how I would go about changing the checked radio button to match it's current image!我知道这是基于我下面的代码的预期行为,我只是想知道如何更改选中的单选按钮以匹配它的当前图像!

Any help would be appreciated!任何帮助,将不胜感激!

constructor(props) {
  super(props);

  this.state = {
      time: 0,
      start: 0,
      currentIndex: 0
  };
  this.setIndex = this.setIndex.bind(this);
  this.startTimer = this.startTimer.bind(this);
  this.resetTimer = this.resetTimer.bind(this);
}

componentDidMount() {
    this.startTimer();
}
componentDidUpdate() {
    if (this.state.time === 5) {
        this.setIndex(this.state.currentIndex + 1);
    }
}
startTimer() {
    this.timer = setInterval(
        () =>
            this.setState({
                time: this.state.time + 1
            }),
        1000
    );
}
resetTimer() {
    this.setState({ time: 0 });
}

setIndex(index, e) {
    console.log("changing");
    if (index < 0) {
        index = headers.length - 1;
    }
    if (index >= headers.length) {
        index = 0;
    }

    this.setState({
        currentIndex: index,
        time: Date.now() - this.state.start
    });
    this.resetTimer();
}

... skipped code ...跳过代码

<div className="carousel">
    <img
       alt="carousel-header"
       className="background-img"
       src={headers[this.state.currentIndex].image}
    />
       <div className="buttons-container">
            <input
                  type="radio"
                   value="1"
                   defaultChecked
                   name="index"
                   onClick={() => this.setIndex(0)}
            ></input>
            <input
                   type="radio"
                   value="2"
                   name="index"
                   onClick={() => this.setIndex(1)}
            ></input>
            <input
                   type="radio"
                   value="3"
                   name="index"
                   onClick={() => this.setIndex(2)}
             ></input>
              <input
                   type="radio"
                   value="4"
                   name="index"
                   onClick={() => this.setIndex(3)}
              ></input>
       </div>
 </div>

You can check currentIndex for automatic change radio buttons.您可以检查currentIndex以获取自动更改单选按钮。

In render functionrender函数中

const { currentIndex } = this.state
      <div className="buttons-container">
        <input
          type="radio"
          value="1"
          defaultChecked
          name="index"
          checked={currentIndex === 0}
          onClick={() => this.setIndex(0)}
        ></input>
        <input
          type="radio"
          value="2"
          name="index"
          checked={currentIndex === 1}
          onClick={() => this.setIndex(1)}
        ></input>
        <input
          type="radio"
          value="3"
          name="index"
          checked={currentIndex === 2}
          onClick={() => this.setIndex(2)}
        ></input>
        <input
          type="radio"
          value="4"
          name="index"
          checked={currentIndex === 3}
          onClick={() => this.setIndex(3)}
        ></input>
       </div>

If I understood correctly, you are using radio buttons which are getting activated onclick (which is default HTML behavior) and are not when using your timer.如果我理解正确的话,您使用的单选按钮会在点击时被激活(这是默认的 HTML 行为),而在使用计时器时则不会。

It is piece of cake, you just need to activate them via JS each time the timer switches background.这是小菜一碟,您只需要在每次计时器切换背景时通过 JS 激活它们。

You probably assumed that your setIndex() function will run everytime your timer makes a change, but that is not a case, since that function only launches when person physically clicks on radio, or onClick event is emitted.您可能假设您的 setIndex() 函数会在您的计时器每次更改时运行,但事实并非如此,因为该函数仅在人们实际点击收音机或发出 onClick 事件时启动。 Because of:因为:

onClick={() => this.setIndex(3)}

To fix it you should modify this piece of code:要修复它,您应该修改这段代码:

startTimer() {
    this.timer = setInterval(
        () =>
            this.setState({
                time: this.state.time + 1
            }),
        1000
    );
}
resetTimer() {
    this.setState({ time: 0 });
}

There you should set the value of radio box, and update the state of index.在那里你应该设置单选框的值,并更新索引的状态。

When resetting remember to reset the state value function and set value of radio box to #1 radio.重置时记得重置状态值功能并将单选框的值设置为#1 radio。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM