【发布时间】:2017-11-11 06:29:06
【问题描述】:
我正在尝试根据复选框选择来呈现组件。我的做法如下:
class ReportMainCat extends Component {
constructor(props) {
super(props);
this.report_next = this.report_next.bind(this);
};
report_next() {
if(this.refs.sexual.checked) {
return <PostOptionBar/>
}
}
render() {
return (
<div className="text_align_left">
<div className="width_100 margin_bottom10px">
<input type="checkbox" ref="sexual"/>
<a>Sexual Content</a>
</div>
<div className="width_100 margin_bottom10px">
<input type="checkbox" ref="selfharm"/>
<a>Threat or self harm</a>
</div>
<div className="width_100 margin_bottom10px">
<input type="checkbox" ref="abuse"/>
<a>Abuse or bullying</a>
</div>
<div className="width_100 margin_bottom10px">
<input type="checkbox" ref="illegal"/>
<a>Illegal substances</a>
</div>
<div className="width_100 margin_bottom10px">
<input type="checkbox" ref="discrimination"/>
<a>Discrimination</a>
</div>
<div className="width_100 margin_bottom10px">
<input type="checkbox" ref="copyright"/>
<a>Copyright or spam</a>
</div>
<div className="width_100 margin_bottom10px">
<input type="checkbox" ref="nopermission"/>
<a>Humiliating, embarassing or posted without permission</a>
</div>
<button className="float_right" onClick={this.report_next}>Next</button>
{this.report_next()}
</div>
)
}
}
我正在检查复选框是否被选中并相应地渲染组件,但我不断收到此错误:
未捕获的类型错误:无法读取未定义的“已检查”属性
我该如何解决这个问题?或者这是做我想做的最好的方法吗?
【问题讨论】:
-
在这一行之前似乎没有构建 ref 对象:{this.report_next()}
-
如果是这样,我该如何解决?
标签: javascript reactjs checkbox