首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >typescript和reactJS的输入问题

typescript和reactJS的输入问题
EN

Stack Overflow用户
提问于 2019-06-13 01:47:43
回答 1查看 35关注 0票数 0
代码语言:javascript
复制
import React, { Component } from 'react'

interface orderInformation {
    customer: number;
    picklePrice: number;
    breadPrice: number;
}

interface ComponentState
{
    customer: number;
    picklePrice: number;
    breadPrice: number;
    error: string;
    finalPickleCost: number;
    finalBreadCost: number;
}

export default class pickleSandwich extends Component<orderInformation,ComponentState> {


    constructor(props: orderInformation) {
        super(props);

        //initializing variables to undefined
        this.state = {
          customer: 0,
          picklePrice: 0,
          breadPrice: 0,
          finalBreadCost:0,
          finalPickleCost:0,
          error: ""
        };
        this.handleChange = this.handleChange.bind(this);
      }


//Get information for the user
    getInfo = orderInformation => {
        orderInformation.preventDefault();

        const { customer, picklePrice, breadPrice } = this.state;

        let pickleCounter = 0;
        let breadCounter = 0;
        if (customer > 0) {
            for(let i = 0; i < customer; i++)
            {
                if( i%3 === 0)
                {
                    pickleCounter = pickleCounter + 2;
                    breadCounter = breadCounter + 3;
                }
                else
                {
                    pickleCounter = pickleCounter + 1;
                    breadCounter = breadCounter + 2;
                }
                this.setState({
                    finalPickleCost: pickleCounter * picklePrice,
                    finalBreadCost: breadCounter * breadPrice,
                    error: ""
                });
            }

          } else {
            this.setState({
              error: "Please enter the values correctly."
            });
        };
    };

    handleChange = e => {
        this.setState({ [e.target.name]: e.target.value } as any
            );
        };


render() {

   // const { customer, finalPickleCost, finalBreadCost } = this.state;

        return (
        <form onSubmit={this.getInfo}>
      <p>Get the information of the order!</p>
      <input
        type="text"
        id="Customers"
        value = "Customers"
        placeholder="Amount of Customers"
        name="Customer"
        onChange={this.handleChange}
        required
      />
      <input
        type="text"
        id="picklePrice"
        placeholder="Price of Pickle"
        value = "picklePrice"
        name="picklePrice"
        onChange={this.handleChange}
        required
      />

      <input
        type="text"
        id="breadBrice"
        placeholder="Price of Bread"
        value = "breadPrice"
        name="breadPrice"
        onChange={this.handleChange}
        required
      />
      <button type="submit">Get Information </button>

    </form>
        );
}
}

每当我运行此命令时,我都不能更改输入字段中的值,输入字段实际上显示了它的值,并且不能更改。由于某些原因,我无法输入任何值,甚至无法删除输入字段中显示的内容。然而,我也希望每当点击按钮时,字段就会变成空的。

EN

回答 1

Stack Overflow用户

发布于 2019-06-13 03:36:23

起作用了,但由于某些原因,当我执行console.log检查值时,我得不到客户。我收到了picklePrice和breadPrice,但没有收到客户:/ -

这是因为状态中输入名称和属性不同:

输入:name="Customer"

状态:customer: 0,

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56567785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档