我找不到在包含数组的对象字段中动态输入值的方法

发布于 2025-02-12 00:20:57 字数 333 浏览 1 评论 0原文

这是对象。我如何通过在react中动态在listItem键中输入fyage falue

 { 
   name: "", 
   dueDate: "", 
   grossAmount: "", 
   billNo: "", 
   billDate: "" ,
   listItem:[{ productName: "", quantity: null, price: null, amount: null, gstRate: null}], 
   gstAmount: null, 
   netAmount: null, 
   notes: "", 
   status: " "
}

this is the object . How can i input value through form tag in listItem key dynamically in react

 { 
   name: "", 
   dueDate: "", 
   grossAmount: "", 
   billNo: "", 
   billDate: "" ,
   listItem:[{ productName: "", quantity: null, price: null, amount: null, gstRate: null}], 
   gstAmount: null, 
   netAmount: null, 
   notes: "", 
   status: " "
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

澜川若宁 2025-02-19 00:20:57

假设对象使用您可以执行的USESTATE挂钩存储在状态中

const [obj,setObj] = useState({ 
       name: "", 
       dueDate: "", 
       grossAmount: "", 
       billNo: "", 
       billDate: "" ,
       listItem:[{ productName: "", quantity: null, price: null, amount: null, gstRate: null}], 
       gstAmount: null, 
       netAmount: null, 
       notes: "", 
       status: " "
    })

。。

状态输入:

const [productName,setProductName] = useState("");

对于输入,您可以执行这样的操作。注意:我只是为了更改productName而这样做,但是您可以为OBJ的任何其他键执行此操作。

<form>
  <input type="text" onChange={(e)=>setProductName(e.target.value)} />
  <button onClick={()=>setObj({...obj,lastItem:[...lastItem[0],productName]})}></button>
</form>

Assuming object is stored in a state using useState hook

const [obj,setObj] = useState({ 
       name: "", 
       dueDate: "", 
       grossAmount: "", 
       billNo: "", 
       billDate: "" ,
       listItem:[{ productName: "", quantity: null, price: null, amount: null, gstRate: null}], 
       gstAmount: null, 
       netAmount: null, 
       notes: "", 
       status: " "
    })

You can do..

state for input:

const [productName,setProductName] = useState("");

And for input you can do something like this. Note: I just did this for changing productName, but you can do this for any of the other keys of the obj.

<form>
  <input type="text" onChange={(e)=>setProductName(e.target.value)} />
  <button onClick={()=>setObj({...obj,lastItem:[...lastItem[0],productName]})}></button>
</form>
你又不是我 2025-02-19 00:20:57

首先将名称属性属于您的输入元素。然后创建一个onChangeHandler函数。此功能可以处理您拥有的每个输入的所有更改。
您的输入(名称属性应与您的对象密钥相同):

<input name='dueDate' onChange={onChangeHandler} type="text" value={myObject.dueDate} />

onChangeHandler函数:

const onChangeHandler = (event) => {
    myObject = {
        return {...myObject, [event.targer.name]: event.targer.value}
    }
}

First add name attribute to your input element. Then create a onChangeHandler function. This function can handle all changes for every inputs you have.
Your inputs (the name attribute should be the same as your object key):

<input name='dueDate' onChange={onChangeHandler} type="text" value={myObject.dueDate} />

onChangeHandler function:

const onChangeHandler = (event) => {
    myObject = {
        return {...myObject, [event.targer.name]: event.targer.value}
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文