![](/img/trans.png)
[英]ReactJS: Select with default value from props without onChange event?
[英]Click event is not triggers from the select onchange event in reactjs
我需要根據選擇的值在react中實現對文件輸入元素的單擊。 我下面的代碼在Linux-chrome上工作正常,但在Windows chrome上無法正常工作。
import React, { Component } from "react";
import ReactDOM from "react-dom";
import "./styles.css";
class App extends Component {
constructor(props) {
super(props);
}
handleChange(e) {
if (e.target.value === "upload") {
this.fileExplorer.click();
}
}
openFileExplorer(e) {
this.fileExplorer.click();
}
render() {
return (
<div className="App">
<button onClick={this.openFileExplorer.bind(this)}>Open</button>
<select onChange={this.handleChange.bind(this)}>
<option key="none">None</option>
<option key="upload">upload</option>
</select>
<input
type="file"
accept=".json"
style={{ display: "none" }}
ref={input => (this.fileExplorer = input)}
/>
</div>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
你可以試試這個嗎?
this.openFileExplorer = this.openFileExplorer.bind(this);
此行應添加構造函數的末尾。
<button onClick={this.openFileExplorer}>Open</button>
<select onChange={this.handleChange}
您應該像這樣更改這些行。 我希望它能工作(我正在使用macos)。 https://reactjs.org/docs/handling-events.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.