首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React中的输出表单字段值

React中的输出表单字段值
EN

Stack Overflow用户
提问于 2017-08-22 00:06:56
回答 2查看 916关注 0票数 1

我目前正在开发我的第一个React应用程序,我正在尝试制作一个简单的表单,它将输出值为一种格式,可以存储为以后使用(可能是JSON文件)。

我尝试过将值输出到警报,而不是直接输出到JSON文件,但即使这样也没有成功。

基本上,我想要的是从表单中获取按下submit按钮时所处状态的值,并将它们输出为可用的格式。

下面是我使用的代码片段。

代码语言:javascript
复制
class PeripheralPage extends React.Component {
    state = {
    peripherals: [
      {
        name: "Product 1",
        installation: 79.99,
        monthly: 5.99,
        count: 1,
        min: 1,
        max: 5,
        perimage: peripheralimage
      },
      {
        name: "Product 2",
        installation: 19.99,
        monthly: 9.99,
        count: 2,
        min: 2,
        max: 12,
        perimage: peripheralimage
      },
      {
        name: "Product 3",
        installation: 19.99,
        monthly: 9.99,
        count: 4,
        min: 3,
        max: 8,
        perimage: peripheralimage
      }
    ]
  };

  onChange = (index, val) => {
    this.setState({
      peripherals: this.state.peripherals.map(
        (name, i) => (i === index ? { ...name, count: val } : name)
      )
    });
  };


  submitPackage = (e) => {
    e.preventDefault();
    var periphs = {PeripheralList}
    var installationCost = {InstallTotal}
    alert('Your Package includes: ' + PeripheralList + installationCost );
  }


  render() {

    return (
      <div>
      <form onSubmit={this.submitPackage.bind(this)}>
        <PeripheralList
          peripherals={this.state.peripherals}
          onChange={this.onChange.bind(this)}
        />
        <InstallTotal ref="installCost" peripherals={this.state.peripherals} />
        <MonthlyTotal peripherals={this.state.peripherals} />
        <input type="submit" value="Save Package" />
        </form>
      </div>
    );
  }
}

const PeripheralList = ({ peripherals, onChange }) =>
  <div>
    {peripherals.map((peripherals, i) =>
      <div key={i}>
        <div className="componentname">{peripherals.name}</div>
        <div className="installcost">Install: £{peripherals.installation}</div>
        <div className="monthlycost">Monthly: £{peripherals.monthly}</div>
        <div className="quantity">
        <input
          type="number"
          min={peripherals.min} 
          max={peripherals.max}
          value={peripherals.count}
          onChange={e => onChange(i, parseInt(e.target.value, 10)|| 0)}
        />
        </div>

      </div>
    )}
  </div>;

const InstallTotal = ({ peripherals }) =>
  <h3>
    Installation Cost:
    £{peripherals.reduce((sum, i) => (sum += i.count * i.installation), 0)}
  </h3>;

const MonthlyTotal = ({ peripherals }) =>
  <h3>
    Monthly Cost:
    £{peripherals.reduce((sum, i) => (sum += i.count * i.monthly), 0)}
  </h3>;

在这件事上,任何帮助都会非常感谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-08-29 15:17:12

弄清楚了,它比我做的要简单,我的新代码如下

代码语言:javascript
复制
import productdata from "./myjsonfile.json";

class productTable extends React.Component {
  render() {
    return (
      <table>
        <tbody>
          <tr>
            <td>
                <span>
                  {productdata.data.productgroup[0].name} 
                </span>
                <span>
                  {productdata.data.productgroup[0].description}
                </span>
                <span>
                  Price: £{
                    productdata.data.productgroup[0].price
                  }
                </span>
            </td>
          </tr>
         <tr>
            <td>
                <span>
                  {productdata.data.productgroup[1].name} 
                </span>
                <span>
                  {productdata.data.productgroup[1].description}
                </span>
                <span>
                  Price: £{
                    productdata.data.productgroup[1].price
                  }
                </span>
            </td>
          </tr>
        </tbody>
      </table>
    );
  }
}

请注意,"data“和"productgroup”是json文件本身中的值,方括号中的数字是这些值所在的产品组。

票数 0
EN

Stack Overflow用户

发布于 2017-08-22 00:11:01

您的输入input需要如下所示的name

代码语言:javascript
复制
<input
          onChange={this.onFormInputChange}
          name ='number_name'
          type="number"
          min={peripherals.min} 
          max={peripherals.max} />

你的onChange应该是这样的:

代码语言:javascript
复制
onFormInputChange(event) {
        let form = this.state.form;
        form[event.target.name] = event.target.value;
        this.setState({ form });
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45801532

复制
相关文章

相似问题

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