javascript - 具有操作和存储功能的单选按钮

标签 javascript reactjs

我正在使用 4 个单选按钮,我需要获取用户选择的 2 个选项并将其发送到套接字,但首先,我需要知道如何使用操作和存储来更新所选选项

这里是您可以看到按钮的代码

class BetBehindModal extends Component {

  constructor (props) {
    super(props);
  }

  render () {
    return (
      <div>
        <div>
          <p>Bet Behind Settings</p>
          <p>When seated player Doubles:</p>
          <form>
            <input type="radio" value="double" name="doubles" /> Always Double my bet <br />
            <input type="radio" value="nodouble" name="doubles" /> Never Double my bet
          <p>When seated player Splits:</p>
            <input type="radio" value="split" name="splits" /> Always Split <br />
            <input type="radio" value="nosplit" name="splits" /> Assign bet to 1st hand
          </form>
          <hr />
          <button className="toggleModalBtn" type="submit" onClick={this._confirm}>Confirm</button>
        </div>
      </div>
    );
  }
}

export default BetBehindModal;

所以,有4个选项,用户有权选择这4个中的2个。我需要将该信息发送到套接字以及Nodejs制作的后端,但最重要的部分是如何工作与这个 Action 和商店?

最佳答案

据我了解这个问题,您很难尝试根据单选按钮更新组件的状态。作为一种方法,您可以添加 onChange 处理程序:

class BetBehindModal extends Component {

  constructor (props) {
    super(props);

    this.onDoublesChange = this.onDoublesChange.bind(this);
    this.onSplitsChange = this.onSplitsChange.bind(this);
  }

  render () {
    return (
      <div>
        <div>
          <p>Bet Behind Settings</p>
          <p>When seated player Doubles:</p>
          <form>
            <input type="radio" value="double" name="doubles" onChange={this.onDoublesChange}/> Always Double my bet <br />
            <input type="radio" value="nodouble" name="doubles" onChange={this.onDoublesChange}/> Never Double my bet
          <p>When seated player Splits:</p>
            <input type="radio" value="split" name="splits" onChange={this.onSplitsChange}/> Always Split <br />
            <input type="radio" value="nosplit" name="splits" onChange={this.onSplitsChange}/> Assign bet to 1st hand
          </form>
          <hr />
          <button className="toggleModalBtn" type="submit" onClick={this._confirm}>Confirm</button>
        </div>
      </div>
    );
  }

  onDoublesChange({ target }) {
    if (target.checked) this.setState({ doubles: target.value });
  }

  onSplitsChange({ target }) {
    if (target.checked) this.setState({ splits: target.value });
  }
}

export default BetBehindModal;

关于javascript - 具有操作和存储功能的单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34223444/

相关文章:

javascript - TinyMCE 仅在页面刷新时加载

javascript - 我如何在 JavaScript 中获取这个子 html 元素?

reactjs - 使用 Typescript 在 React 项目中存储/检索配置设置

javascript - 我的新 Reactjs 应用程序出现 404 错误,它是否正在寻找 main.js 文件?

javascript - 如何将图像从 JSON 数据导入到 create-react-app

reactjs - react中基于路由的条件渲染组件

javascript - 从 react js中的对象数组中删除一个元素

javascript - 等待重载方法

javascript - 如何从 Angular 中的 JS 文件调用方法?

javascript - 时刻 js 未定义 Laravel