简体   繁体   English

React HTML Select标签不允许未选择的值

[英]React html select tag not allowing unselected value

I've create a new component in React that contains a <select> tag. 我已经在React中创建了一个包含<select>标签的新组件。 As I want it displayed as a list, not a dropdown, I've set it's size to 10. It is being rendered as expected and I have set it's value property to a state.value property as below. 正如我希望将其显示为列表而不是下拉列表一样,我将其大小设置为10。正在按预期方式呈现它,并将其value属性设置为state.value属性,如下所示。

render() {
    return (
        <select className="list" size="10" value={this.state.value} onChange={this.handleChange} >
        { this.props.paraList.map(i => 
            <option key={i.ID} value={i.ID} onDoubleClick={this.handleDblClick}>{i.Text}</option>
        )}
        </select>
    );
}

However, when the user doubleclick an item in the list, I remove that item. 但是,当用户双击列表中的一个项目时,我将其删除。 In the this.handleDblClick method I am setting the state.value to 0, but the first item in the list appears as selected. this.handleDblClick方法中,我将state.value设置为0,但是列表中的第一项显示为选中状态。 If I check the state.value property, it is correctly assigned to 0, and the select also has it's value as 0. But the appearance of the first item being selected stays there. 如果我检查state.value属性,则将其正确分配为0,并且select的值也为0。但是,所选择的第一项的外观保持不变。 I have also tried setting the value to whitespace, null, -1, nothing works. 我也尝试将值设置为空白,null,-1,没有任何效果。 Here is how the list appears for the user after a value is set to 0 (or null, or whitespace, or -1): 将值设置为0(或null,空白或-1)后,列表显示给用户的方式如下:

First item is not really selected here 此处未真正选择第一项

The most annoying thing here is that, if the user wants to select the first item when the list is in this situation, they have to select another item and then click back on the first item. 这里最令人讨厌的是,如果用户希望在列表处于这种情况下选择第一项,则他们必须选择另一项,然后单击第一项。 Is there a way to not having any item selected in this list at one time? 有没有办法一次不在此列表中选择任何项目?

I believe I found the problem. 我相信我找到了问题。 I need to set the state.value to undefined to clear the current selection of the list. 我需要将state.value设置为undefined以清除列表的当前选择。

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

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