Saya memiliki bidang teks. Jika menyatakan nol tetapi nilai bidang teks ditampilkan di Bidang Teks

<TextField
                        style={{ width: '65%'}}
                        id="standard-search"
                        label="Residential Address"
                        type="text"
                        margin="dense"
                        variant="outlined"
                        name="ResidentialAddress"
                        onChange={(e)=>this.handleInputChange(e)}
                    />
0
Zulqurnain Huda 30 Oktober 2019, 12:39

2 jawaban

Jawaban Terbaik

Tentukan nilai Anda seperti ini value={this.state.input_value}

<TextField
  style={{ width: '65%'}}
  id="standard-search"
  label="Residential Address"
  type="text"
  margin="dense"
  variant="outlined"
  name="ResidentialAddress"
  onChange={(e)=>this.handleInputChange(e)}
  value={this.state.input_value}
/>
1
akhtarvahid 30 Oktober 2019, 09:42

Anda perlu mengikat kotak teks value ke properti negara. Berdasarkan jenis komponen yang Anda gunakan, Anda dapat mencoba opsi di bawah ini.

Untuk komponen kelas:

class YouComponentName extends Component {
  constructor (props, context) {
    super(props, context)
    this.state = {
      yourTextBoxValue: ""
    }
}

  handleInputChange (event) {
    this.setState({
      yourTextBoxValue: event.target.value;
    });
  }

render(){
    <>
        <TextField
            style={{ width: '65%'}}
            id="standard-search"
            label="Residential Address"
            type="text"
            margin="dense"
            variant="outlined"
            name="ResidentialAddress"
            onChange={(e)=>this.handleInputChange(e)}
            value = {this.state.yourTextBoxValue}
        />
    </>
}

Untuk komponen fungsi (menggunakan kait Bereaksi):

function YourComponentName(props) {
    const [yourTextBoxValue, setYourTextBoxValue] = useState("")

    const handleInputChange = (event) => {
        this.setState({
            setYourTextBoxValue(event.target.value);
        });
    }

    return (
        <>
            <TextField
                style={{ width: '65%'}}
                id="standard-search"
                label="Residential Address"
                type="text"
                margin="dense"
                variant="outlined"
                name="ResidentialAddress"
                onChange={handleInputChange}
                value = {yourTextBoxValue}
            />
        </>
    )
}
0
Souvik Ghosh 30 Oktober 2019, 10:06