簡體   English   中英

如何增加或減少數組的多個輸入的值?

[英]How to increase or decrease the values ​of multiple inputs of array?

就是這樣了:

const [count, setCount] = useState(1 as any)

{items.map((item: any) => (...

然后將數組的項目映射到購物車中。 我希望能夠增加或減少數量。

<input
 type="number"
 min="1"
 value={count}
 onChange={(event): any => {
 setCount(event.target.value)
 }}
/>

它改變了所有項目的價值。 但我想獨立更改元素的值。

試試這個方法

const [items, setItems] = useState(items); // items array []

const onTextChanged = (index, value) => {
    const items = [...items]; 
    items[index].count = value; <-- "count" is example key for showing way out of this -->
    setItems(items);
}

{items.map((item, index) => (

 <input
  type="number"
  min="1"
  value={item.count || 0}
  onChange={(event): any => {
    onTextChanged(index, event.target.value)
  }}
 />

)}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM