无法在 ReactJS 上使用 defaultValue 编辑表单

代码爱好者

我想用 reactJS 更新我的注册表,当我用 Postman 运行我的后端时,它运行良好。

我的问题是关于前端,当我运行它时,通过编辑输入字段,我收到警报,之后无法发布。

我的课 :

class EditClient extends Component {
  constructor(props) {
    super(props)
    this.state = {
      clients: [],
      Code: props.match.params.Code,
      Prenom: '',
      Nom: '',
      FAX: '',
      Telephone: '',
      Email: '',
      Adresse1: '',
      Adresse2: ''
    }
    this.handleEdit = this.handleEdit.bind(this);
  }
  componentDidMount() {
    axios.get('http://localhost:4000/app/viewclient/' + this.props.match.params.Code).then(response => {
      if (response && response.data) {
        this.setState({ clients: response.data });
      }
    }).catch(error => console.log(error));
  }
  handleEdit() {
    if (this.state.FAX.length == 8 && this.state.Telephone.length == 8) {
      var client = {
        Prenom: this.state.Prenom,
        Nom: this.state.Nom,
        FAX: this.state.FAX,
        Telephone: this.state.Telephone,
        Email: this.state.Email,
        Adresse1: this.state.Adresse1,
        Adresse2: this.state.Adresse2
      }
      axios({
        method: 'put',
        url: "http://localhost:4000/app/editclient/" + this.props.match.params.Code,
        data: client,
      }).then(function(response) {
        this.setState({ Prenom: "" });
        this.setState({ Nom: "" });
        this.setState({ FAX: "" });
        this.setState({ Telephone: "" });
        this.setState({ Email: "" });
        this.setState({ Adresse1: "" });
        this.setState({ Adresse2: "" });
      }.bind(this)).catch(function(error) {
        console.log(error);
      });
    }
    else {
      alert("Input field value is missing");
    }
  }
  handleCodeChange = (e) => {
    this.setState({ Code: e.target.value });
  }
  handlePrenomChange = (e) => {
    this.setState({ Prenom: e.target.value });
  }
  handleNomChange = (e) => {
    this.setState({ Nom: e.target.value });
  }
  handleFAXChange = (e) => {
    this.setState({ FAX: e.target.value });
  }
  handleTelephoneChange = (e) => {
    this.setState({ Telephone: e.target.value });
  }
  handleEmailChange = (e) => {
    this.setState({ Email: e.target.value });
  }
  handleAdresse1Change = (e) => {
    this.setState({ Adresse1: e.target.value });
  }
  handleAdresse2Change = (e) => {
    this.setState({ Adresse2: e.target.value });
  }
  render() {
    let { clients } = this.state;
    var btn = {
      display: 'block',
      margin: 'auto'
    }
    var Cd = { pointerEvents: 'none' }
    return (<div className="animated fadeIn">

           <Row>
          <Col xs="12"  >

              <Card>
                <CardHeader>
              <h4><strong>    <i className="fa fa-user"> </i> Modifier le client  </strong></h4>

                </CardHeader>

                  <CardBody>
                  {   this.state.clients.map(client => (
                    <Form className="form-horizontal"  method="POST"  key={client.Code} >


                   <FormGroup row >
                       <Col md="3">
                    <h5>  <Label htmlFor="hf-id"><strong>Code client</strong></Label></h5>
                    </Col>

                      <Col xs="12" md="9" >
                        <div className="controls">
                          <InputGroup className="input-code">
                            <InputGroupAddon addonType="prepend">
                              <InputGroupText><i className="fa fa-id-card-o" aria-hidden="true"></i></InputGroupText>
                            </InputGroupAddon>
                            <Input id="Code" style = { Cd } size="16" type="text" placeholder="Entrer le code du client"   onChange={this.handleCodeChange.bind(this)} defaultValue={client.Code}     readonly="readonly"/>
                            <FormFeedback className="help-block">Entrez le code SVP ! </FormFeedback>
                          </InputGroup>
                        </div>
                        </Col>
                      </FormGroup>

                      <FormGroup row>
                       <Col md="3">
                    <h5>  <Label htmlFor="hf-pren"><strong>Prénom</strong></Label></h5>
                    </Col>
                    <Col xs="12" md="9">
                        <div className="controls" >
                          <InputGroup className="input-pren">
                            <InputGroupAddon addonType="prepend">
                              <InputGroupText><i className="fa fa-user-circle-o" aria-hidden="true"></i></InputGroupText>
                            </InputGroupAddon>
                            <Input id="prénom" size="16" type="text"    defaultValue={client.Prenom}  onChange={this.handlePrenomChange.bind(this)} placeholder="Entrer le prénom du client" autoComplete='given-name' />

                          </InputGroup>

                        </div>
                        </Col>
                      </FormGroup>

                      <FormGroup row>
                       <Col md="3">
                    <h5>  <Label htmlFor="hf-nom"><strong>Nom</strong></Label></h5>
                    </Col>
                    <Col xs="12" md="9">
                        <div className="controls">
                          <InputGroup className="input-nom">
                            <InputGroupAddon addonType="prepend">
                              <InputGroupText><i className="fa fa-user-circle" aria-hidden="true"></i></InputGroupText>
                            </InputGroupAddon>
                            <Input id="nom" size="16" type="text"    defaultValue={client.Nom}     onChange={this.handleNomChange.bind(this)} placeholder="Entrer le nom du client" autocomplete='family-name' />

                          </InputGroup>

                        </div>
                        </Col>
                      </FormGroup>


                     <FormGroup row>
                       <Col md="3">
                    <h5>  <Label htmlFor="hf-tel"><strong>Numéro de FAX</strong></Label></h5>
                    </Col>
                    <Col xs="12" md="9">
                        <div className="controls">
                          <InputGroup className="input-fax">
                            <InputGroupAddon addonType="prepend">
                              <InputGroupText><i className="flag-icon flag-icon-tn " title="tn" id="tn"></i></InputGroupText>
                            </InputGroupAddon>
                            <Input id="fax" size="16" type="tel"   defaultValue={client.FAX}   onChange={this.handleFAXChange.bind(this)} placeholder="Entrer le numéro du FAX du client" autoComplete='tel' />

                          </InputGroup>

                        </div>
                        </Col>
                      </FormGroup>
                      <FormGroup row>
                       <Col md="3">
                    <h5>  <Label htmlFor="hf-tel"><strong>Numéro de Téléphone</strong></Label></h5>
                    </Col>
                    <Col xs="12" md="9">
                        <div className="controls">
                          <InputGroup className="input-tel">
                            <InputGroupAddon addonType="prepend">
                              <InputGroupText><i className="flag-icon flag-icon-tn " title="tn" id="tn"></i></InputGroupText>
                            </InputGroupAddon>
                            <Input id="tel" size="16" type="tel"   defaultValue={client.Telephone}   onChange={this.handleTelephoneChange.bind(this) } placeholder="Entrer le numéro du télèphone du client" autoComplete='tel-national' />

                          </InputGroup>

                        </div>
                        </Col>
                      </FormGroup>


                      <FormGroup row>
                       <Col md="3">
                    <h5>  <Label htmlFor="hf-email"><strong>Email</strong></Label></h5>
                    </Col>
                    <Col xs="12" md="9">
                        <div className="controls">
                          <InputGroup className="input-email">
                            <InputGroupAddon addonType="prepend">
                              <InputGroupText><i className="fa fa-envelope" aria-hidden="true"></i></InputGroupText>
                            </InputGroupAddon>
                            <Input id="email" size="16"      defaultValue={client.Email}   onChange={this. handleEmailChange.bind(this)}type="email" placeholder="Entrer l'email du client" autoComplete='email' />

                          </InputGroup>

                        </div>
                        </Col>
                      </FormGroup>


                       <FormGroup row>
                       <Col md="3">
                    <h5>  <Label htmlFor="hf-adr"><strong>Adresse 1 </strong></Label></h5>
                    </Col>
                    <Col xs="12" md="9">
                        <div className="controls">
                          <InputGroup className="input-Adr">
                            <InputGroupAddon addonType="prepend">
                              <InputGroupText><i className="fa fa-location-arrow" aria-hidden="true"></i></InputGroupText>
                            </InputGroupAddon>
                            <Input id="adr" size="16" type="text"    defaultValue={client.Adresse1}  onChange={this.handleAdresse1Change.bind(this)} placeholder="Entrer l'adresse 1  du client" autocomplete='street-address' />

                          </InputGroup>

                        </div>
                        </Col>
                      </FormGroup>

                       <FormGroup row>
                       <Col md="3">
                    <h5>  <Label htmlFor="hf-adr"><strong>Adresse 2 </strong></Label></h5>
                    </Col>
                    <Col xs="12" md="9">
                        <div className="controls">
                          <InputGroup className="input-Adr2">
                            <InputGroupAddon addonType="prepend">
                              <InputGroupText><i className="fa fa-location-arrow" aria-hidden="true"></i></InputGroupText>
                            </InputGroupAddon>
                            <Input id="adr2" size="16" type="text"   defaultValue={client.Adresse2}  onChange={this.handleAdresse2Change.bind(this)} placeholder="Entrer l'adresse 2  du client" autoComplete='address-line2' />

                          </InputGroup>

                        </div>
                        </Col>
                      </FormGroup>


                      <div className="form-actions" >

                        <Button  active color="info" size="lg"    style={btn}  type="submit"  onClick={() => this. handleEdit()} >Modifier</Button>

                      </div>
                    </Form>
                  ))}
                  </CardBody>

              </Card>

          </Col>

        </Row>

        </div>);
  }
}
export default EditClient;

我的路由器:

exports.editclient = function(req, res) {
    var data = {
        Prenom: req.body.Prenom,
        Nom: req.body.Nom,
        FAX: req.body.FAX,
        Telephone: req.body.Telephone,
        Email: req.body.Email,
        Adresse1: req.body.Adresse1,
        Adresse2: req.body.Adresse2,
    };
    var Code = req.params.Code
    connection.query("UPDATE clients set ? WHERE Code = ? ", [data, req.params.Code], function(error, results, fields) {
        if (error) throw error;
        else {
            res.send(JSON.stringify(results));
            console.log("Data is updated");
        }
    });
};

我的服务器:

router.put('/editclient/:Code', clients.editclient);

这似乎是一个与 defaultValue 相关的问题,因为当我使用value = {this.state.Code}和其他输入相同时,我可以修改并且我没有收到任何错误。

我也尝试过value ={client.Telephone},但我无法更改输入字段。

请问我该如何解决?

赛勒斯·蔡

Okej,首先你不能这样做

this.setState({ Prenom: "" });
this.setState({ Nom: "" });
this.setState({ FAX: "" });
this.setState({ Telephone: "" });
this.setState({ Email: "" });
this.setState({ Adresse1: "" });
this.setState({ Adresse2: "" });

您将重新渲染七次。让它像一个对象,就像这样:

this.setState({
  Prenom: "",
  Nom: "",
  FAX: "",
  Telephone: "",
  Adresse1: "",
  Adresse2: ""
})

那么你可以制作一个函数来获取所有的值。我通常添加两个参数。所以不要像这样:

handleCodeChange = (e) => {
    this.setState({ Code: e.target.value });
  }
  handlePrenomChange = (e) => {
    this.setState({ Prenom: e.target.value });
  }
  handleNomChange = (e) => {
    this.setState({ Nom: e.target.value });
  }
  handleFAXChange = (e) => {
    this.setState({ FAX: e.target.value });
  }
  handleTelephoneChange = (e) => {
    this.setState({ Telephone: e.target.value });
  }
  handleEmailChange = (e) => {
    this.setState({ Email: e.target.value });
  }
  handleAdresse1Change = (e) => {
    this.setState({ Adresse1: e.target.value });
  }
  handleAdresse2Change = (e) => {
    this.setState({ Adresse2: e.target.value });
  }

这样做更像这样并保持干燥(不要重复自己)

handleFormchange = (e, name) => {
  this.setstate({[name] : e.target.value})
}

然后当涉及到表单时,您将为其分配一个默认值,因此每次重新渲染时,它都会在表单上设置默认值。你需要的是 onChange 事件:

class Me extends Component {
  constructor(props) {
    super(props);
    this.state = { foo: "" };
  }

  handleFormChange = (e, name) => {
    this.setState({ [name]: e.target.value });
  };
  render() {
    return (
      <input
        type="text"
        value={this.state.foo}
        onChange={e => this.handleFormChange(e, "foo")}
      />
    );
  }
}

这是一个片段:

片段

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

可编辑的表单 ReactJS

来自分类Dev

无法提交表单 - ReactJs

来自分类Dev

如何为表单onSubmit事件上的输入重置defaultValue属性?

来自分类Dev

无法使用 React 中的 defaultValue 属性在 <select> 上呈现默认值

来自分类Dev

无法使用reactjs将文件上传为我的表单的路径

来自分类Dev

提交时表单无法重置 ReactJS Flux

来自分类Dev

使用DefaultValue.Mock自动嵌套模拟无法按预期工作

来自分类Dev

默认图片无法使用 ReactJs

来自分类Dev

无法显示数据库中的内容以使用CodeIgniter 3在表单中进行编辑

来自分类Dev

我无法从jquery编辑和提交表单

来自分类Dev

无法用数据填充“编辑表单”

来自分类Dev

使用Django Bootstrap表单编辑Django表单

来自分类Dev

无法在ReactJS上设置模态动画

来自分类Dev

使用Django表单编辑行

来自分类Dev

Richtext / CK编辑器无法在Android上使用

来自分类Dev

无法使遮罩与输入上的“保存/编辑”配合使用

来自分类Dev

JCE编辑器无法在Joomla上使用

来自分类Dev

ReactJS表单输入-在验证函数中无法获取“ const”变量

来自分类Dev

ReactJS表单输入-在验证函数中无法获取“ const”变量

来自分类Dev

无法在几个 ReactJs 表单输入字段中插入数据

来自分类Dev

ReactJS Semantic ui - 当值属性存在时无法输入表单

来自分类Dev

getDefaultProps无法使用reactjs正常工作

来自分类Dev

我无法使用reactjs输入输入文本

来自分类Dev

无法使用require()在Reactjs中导入图片

来自分类Dev

无法使用AJAX表单

来自分类Dev

使代码在创建/编辑表单上保持干燥

来自分类Dev

Sonata + FOSUserBundle在编辑表单上编辑角色

来自分类Dev

无法使用Javascript在Google表单上设置输入值

来自分类Dev

无法使用JavaScript在表单上显示日期选择器

Related 相关文章

  1. 1

    可编辑的表单 ReactJS

  2. 2

    无法提交表单 - ReactJs

  3. 3

    如何为表单onSubmit事件上的输入重置defaultValue属性?

  4. 4

    无法使用 React 中的 defaultValue 属性在 <select> 上呈现默认值

  5. 5

    无法使用reactjs将文件上传为我的表单的路径

  6. 6

    提交时表单无法重置 ReactJS Flux

  7. 7

    使用DefaultValue.Mock自动嵌套模拟无法按预期工作

  8. 8

    默认图片无法使用 ReactJs

  9. 9

    无法显示数据库中的内容以使用CodeIgniter 3在表单中进行编辑

  10. 10

    我无法从jquery编辑和提交表单

  11. 11

    无法用数据填充“编辑表单”

  12. 12

    使用Django Bootstrap表单编辑Django表单

  13. 13

    无法在ReactJS上设置模态动画

  14. 14

    使用Django表单编辑行

  15. 15

    Richtext / CK编辑器无法在Android上使用

  16. 16

    无法使遮罩与输入上的“保存/编辑”配合使用

  17. 17

    JCE编辑器无法在Joomla上使用

  18. 18

    ReactJS表单输入-在验证函数中无法获取“ const”变量

  19. 19

    ReactJS表单输入-在验证函数中无法获取“ const”变量

  20. 20

    无法在几个 ReactJs 表单输入字段中插入数据

  21. 21

    ReactJS Semantic ui - 当值属性存在时无法输入表单

  22. 22

    getDefaultProps无法使用reactjs正常工作

  23. 23

    我无法使用reactjs输入输入文本

  24. 24

    无法使用require()在Reactjs中导入图片

  25. 25

    无法使用AJAX表单

  26. 26

    使代码在创建/编辑表单上保持干燥

  27. 27

    Sonata + FOSUserBundle在编辑表单上编辑角色

  28. 28

    无法使用Javascript在Google表单上设置输入值

  29. 29

    无法使用JavaScript在表单上显示日期选择器

热门标签

归档