本文介绍了React 中有条件地禁用选择选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我构建了自己的 React 选择组件,并希望有一个选项可以将默认值设置为 disabled
I built my own React select component and would like to have an option to set the default value to be disabled
组件基本上是这样的:
<select id={this.props.id} name={this.props.name} value={this.props.value} defaultValue={this.props.default} onClick={this.handleFieldClick} onChange={this.handleFieldChange} > <option value="" disabled={this.defaultDisabled ? true : false} >{this.props.defaultLabel}</option> { Object.keys(this.props.options).map((val, i) => ( <option key={i} value={val}>{this.props.options[val]}</option> ))} </select>这一行给我的问题如下:
This line giving me the issue is the following:
<option value="" disabled={this.defaultDisabled ? true : false} >{this.props.defaultLabel}</option>禁用"选项仍然可以选择并呈现如下:
The "disabled" option is still selectable and renders like this:
<option value="" selected="">Default Option</option>我相信这是因为禁用选项的正确语法是
更多推荐
React 中有条件地禁用选择选项
发布评论