我創建了一個保存輸入資料的按鈕,但是當我單擊它時,什么也沒有發生。這是代碼。
onChange = () => {
if (this.state.filesContentDict[this.props.iniType]) {
this.props.changeInitFileParams(this.props.market, this.props.iniType, this.state.filesContentDict[this.props.iniType]);
this.setState({ isNeedOpenInfoWindow: true });
}
}
<form onSubmit={(e) => {
e.preventDefault()
this.onChange()
}}>
<div height="200%" style={{
margin: '20px 0px 0px 40px'
}}><input type="submit" value="Ok" className="c4t-button" height="200%" size="50px" /></div>
</form>
uj5u.com熱心網友回復:
我認為你應該像這樣改變你的 onSubmit
onsubmit={(event)=> onChange(event)}
然后在 onChange => 上使用此代碼
const onChange = (event) => {
e.preventDefault();
if (this.state.filesContentDict[this.props.iniType]) {
this.props.changeInitFileParams(this.props.market, this.props.iniType,
this.state.filesContentDict[this.props.iniType]);
this.setState({ isNeedOpenInfoWindow: true });
}
}
uj5u.com熱心網友回復:
您收到錯誤的主要原因是您沒有使用按鈕。您正在使用輸入標簽。改變
<button type="submit" className="c4t-button" height="200%" size="50px">Ok</button>
uj5u.com熱心網友回復:
以下與您的代碼類似的代碼片段顯示,在<input type='submit' />沒有看到您的代碼的情況下單擊時警報確實會運行,可能存在其他問題,或者 this.state 不是您認為它在該函式中的狀態(范圍不正確或只是在當時它是false所以它不會運行 if 陳述句中的內容)。
class App extends React.Component {
constructor() {
super();
this.state = {
something: {}
}
}
onConfirm = () => {
alert('hi');
}
render() {
return (
<form
style={{ background: 'green', height: 300 }}
onSubmit={(e) => {
e.preventDefault();
this.onConfirm();
}}
>
<input type='submit' value='Ok' />
</form>
);
}
}
ReactDOM.render(
<App />,
window.root
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id='root'></div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/447821.html
標籤:javascript 反应
