【问题标题】:Radio button checked not working in React选中的单选按钮在 React 中不起作用
【发布时间】:2017-11-05 12:22:26
【问题描述】:

我无法让“checked”属性在 React 中的单选按钮上工作。请注意,当我单击单选按钮时,类属性正确更新为“已选择”,因此“activeRating.time_felt_right === false”检查正在工作。

<input type="radio" value="false"
  id={`time_felt_right_${activeRating.id}_false`}
  checked={activeRating.time_felt_right === false}
  className={activeRating.time_felt_right === false ? 'selected' : null}
  onChange={e => {
      console.log('false');
      e.preventDefault()
      updateActiveRating({ time_felt_right: false })
    }
  }
/>

activeRating 由 Redux 的 mapStateToProps 提供。 updateActiveRating 定义如下:

const mapDipsatchToProps = (dispatch) => ({
  updateActiveRating (rating) {
    dispatch(updateActiveRatingAction(rating));
  },
});

【问题讨论】:

  • 尝试为复选框提供“名称”属性
  • 感谢@DayanMorenoLeon 的想法。我添加了一个名字,但它仍然不起作用。
  • activeRating 是什么,updateActiveRating 的来源是什么?请添加所有相关代码
  • 谢谢@AndyRay。更新了!
  • e.preventDefault() 可能是罪魁祸首...尝试删除那个

标签: reactjs


【解决方案1】:

如果您删除 e.preventDefault() 它应该可以工作

【讨论】:

    猜你喜欢
    • 2017-07-13
    • 1970-01-01
    • 2013-06-22
    • 1970-01-01
    • 2011-04-06
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多