我的 React.js 应用程序中有 2 个<select>列表。它们渲染得很好,并且附加到一个handleChange函数上。它们被编程为最初默认为第一个选项。当用户选择另一个选项时,列表在视觉上似乎保留第一个选项。但是,当我提交表单并提醒状态中的关联值时,它会提醒用户选择的值。这是我的渲染函数:<div id="userActions"> <form id="userActionsForm" onSubmit={this.handleSubmit}> <label htmlFor="operator">Operator:</label> <select id="operator" name="operator" value={this.state.operator} onChange={this.handleChange} > {this.state && this.state.operators && this.state.operators.map((op) => <option value={op}>{op}</option>)} </select> <label htmlFor="user">User:</label> <select id="user" name="user" value={this.state.user} onChange={this.handleChange} > {this.state && this.state.testUsers && this.state.testUsers.map((tu) => <option value={tu}>{tu}</option>)} </select> <div className="submit"> <input type="submit" value="Submit" /> </div> <span className="error">{this.state.error}</span> </form></div>这是我的onChange功能:handleChange(event) { this.state.value = event.currentTarget.value; if (event.currentTarget.id === 'operator') { this.state.operator = event.currentTarget.value; } else if (event.currentTarget.id === 'user') { this.state.user = event.currentTarget.value; }}这是我的onSubmit:handleSubmit(event) { alert(this.state.user); alert(this.state.operator); event.preventDefault();}
添加回答
举报
0/150
提交
取消