简体   繁体   English

如何将值从下拉列表传递到选择的输入? 响应式JS

[英]How to pass values from dropdown to inputs on select ? ReactJS

I don't know how to pass values from dropdown on select to 4 inputs.我不知道如何将选择下拉列表中的值传递给 4 个输入。 My dropdown showing address as it should be but I struggle to pass each value to separate inputs.eg address_1 should be in one input where is flat number, address_2 where is street etc.我的下拉列表显示了地址,但我很难将每个值传递给单独的输入。例如,address_1 应该在一个输入中,其中是平面数字,address_2 是街道等。

 const [select,setSelect]=useState()

 <select onChange={(e) => setSelect(e.target.value)}>  

 <option>Select Address</option>
                    {getAddressData?.address? (
                      getAddressData?.address?.map((address: any, id) => (
                        <option>{`${address.Address_1},${address.Address_2}, ${address.Town}, ${address.County}`}</option>

                     <input>

                    type={'text'}
                    name={'flatNumber'}
                    placeholder="Flat Number"
                    value={select}

                   </input>

Right now I have whole line of address eg (30 Ballot Street,Smethick,West Midlands) in the whole one input.现在我在整个输入中输入了整行地址,例如(30 Ballot Street,Smethick,West Midlands)。

Here's the working code.这是工作代码。 It's live here: https://codesandbox.io/s/competent-firefly-0qdrij?file=/src/App.js它就在这里: https ://codesandbox.io/s/competent-firefly-0qdrij?file=/src/App.js

import React, { useState, useEffect } from "react";

function App() {
  const [address, setAddress] = useState({
    Address_1: "",
    Address_2: "",
    Town: "",
    pafCounty: ""
  });

  let getAddressData = {};
  getAddressData.address = [
    {
      Address_1: "Address_1",
      Address_2: "Address_2",
      Town: "Town",
      pafCounty: "pafCounty"
    },
    {
      Address_1: "Address_11",
      Address_2: "Address_22",
      Town: "Town2",
      pafCounty: "pafCounty2"
    }
  ];

  function handleChange(e) {
    setAddress({ ...address, [e.target.name]: e.target.value });
  }

  useEffect(() => {
    console.log(address);
  }, [address]);

  return (
    <>
      <select
        onChange={(e) => {
          setAddress(getAddressData?.address[e.target.value]);
        }}
      >
        <option selected disabled>
          Select Address
        </option>
        {getAddressData?.address?.map((address, index) => (
          <option
            key={index}
            value={index}
          >{`${address.Address_1}, ${address.Address_2}, ${address.Town}, ${address.pafCounty}`}</option>
        ))}
      </select>

      {Object.keys(address).map((key, index) => (
        <input
          type="text"
          name={key}
          placeholder="Flat Number"
          value={address[key]}
          onChange={handleChange}
        />
      ))}
    </>
  );
}

export default App;

Instead of setSelect(e.target.value) , set it to the original structured address object.而不是setSelect(e.target.value) ,将其设置为原始结构化地址对象。 You could have the value of the option be the index within the address list, for example, and then setSelect(getAddressData?.address[2]) or whatever.例如,您可以将选项的值设为地址列表中的索引,然后setSelect(getAddressData?.address[2])或其他任何值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM