我在项目中使用 antd 。代码是:
<FormItem style={{'marginLeft':'-28px', 'marginTop':'10px'}}
>
{getFieldDecorator('invoiceNumber', {
initialValue: this.state.TxnNumber ? this.state.TxnNumber : "#",
rules: [{
required: false, message: 'Please Input Invoice!',
}],
})(
<Input placeholder="S.O.NO#" style={{'width':'120px','height':'28px' }} onChange={(e)=>{e.preventDefault(); e.stopPropagation();
this.handleChange(0,e, 'invoiceNumber')}} />
)}
</FormItem>
handleChange = (index, e, field) => {
if(field == 'invoiceNumber')
this.state.TxnNumber = e.target.value;
}
我希望符号"#"作为文本框中的初始值。还可以防止返回空间删除"#"符号。如何在React中进行操作?
只需使用受控的输入,就可以控制处理程序方法中输入的初始值:
class App extends React.Component {
constructor() {
super();
this.state = {
value: '#'
}
}
handleChange = ({ target: { value } }) => {
this.setState({ value: value || '#' })
}
render(){
const { value } = this.state;
return (
<input
value={value}
onChange={this.handleChange}
/>
)
}
}
ReactDOM.render(<App />, document.getElementById('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>