簡體   English   中英

為什么我的文本輸入不允許我更改其中的值?

[英]Why won't my text input allow me to change the value within it?

我在反應中定義了以下輸入字段:

const [products, setProducts] = useState(cart.products);

<div>
   {products.map((item) => (
    <input
       className="form-control-sm text-center mb-2"
       type="number"
       value={item.quantity}
       onChange={() => handleChange(event.target)}
       name="quantity"
    />
   ))}
</div>

即使未將 item.quantity 指定為只讀字段,我也無法在瀏覽器中更改它的值。

數據:

cart 
 {_id: "5f15ee2c0b94bf240470d70d", user: "5f15ee2c0b94bf240470d70c", 
products: Array(3), __v: 0}
products: Array(3)
0:
product:
brand: "Apple"
category: []
color: "Silver"
images: ["latest-prduct-1.jpg"]
list_price: 220
name: "lorem ippsum dolor"
price: 120
rating: "5"
size: "Medium"
_id: "5f1864667c213e0f5779ee40"
__proto__: Object
quantity: 8
_id: "5f1f04267e422c266c5c1d42"

item.quantity 是指 object 中的固定值。 意味着您為輸入提供固定值,而不是可變的 state。 這是創建受控輸入字段https://reactjs.org/docs/forms.html的反應方式

在鈎子中,就像

const YourComp = ({initialQuantity}) => {
  const [quantity, setQuantity] = useState(initialQuantity)
  return (
    <div>
        <input
           className="form-control-sm text-center mb-2"
           type="number"
           value={quantity}
           onChange={ e => setQuantity(e.target.value)}
           name="quantity"
        />
    </div>
  )

}

而你 map 它為

{cart.products.map((item) => (
    <YourComp initialQuantity={item.amount} />
))}

為什么元素不能放在我的<div>出現?</div><div id="text_translate"><p> 就這么簡單:</p><pre> {this.state.dash? &lt;div className='dashWrapper slide'&gt; HELLO &lt;/div&gt;: null}</pre><p> 無論我在這個 div 中放什么,都沒有出現。 我真的很生氣,因為我已經做了很長時間了,我看不出有任何理由這不起作用。 div 本身顯示得非常好,但里面的任何東西都不是 go。</p><p> CSS:</p><pre> .dashWrapper { width: 100%; height: 100vh; background: white; }.slide { position: absolute; bottom: -100%; -webkit-animation: slide 1s forwards; animation: slide 1s forwards; -webkit-animation-delay: 1s; animation-delay: 1s; -webkit-animation-duration: 1.5s; animation-duration: 1.5s; } @-webkit-keyframes slide { 100% {bottom: 0;} } @keyframes slide { 100% {bottom: 0;} }</pre><p> 在我打破我的鍵盤之前請有人幫助我:(</p><p> 完整的 JSX</p><pre> &lt;div className='pageWrapper'&gt; &lt;div className='slides'&gt; &lt;div className='one' grey={this.state.grey}&gt; &lt;div className='chineseHeader'/&gt; &lt;div className='englishHeader'/&gt; &lt;div className='mePhoto'/&gt; &lt;div className='featuredWorkDivider'/&gt; &lt;div className='dash' onMouseDown={() =&gt; this.setState({grey: '1', dash: true})}/&gt; &lt;div className='allWorksDivider'/&gt; &lt;div className='allWorks'&gt; &lt;h1 className='work' onMouseDown={() =&gt; this.setState({grey: '1', busy: true})}&gt;Busy Map&lt;/h1&gt; &lt;h1 className='work' onMouseDown={() =&gt; this.setState({grey: '1', oke: true})}&gt;Oke&lt;/h1&gt; &lt;h1 className='work' onMouseDown={() =&gt; this.setState({grey: '1', scav: true})}&gt;Scav Hunt&lt;/h1&gt; &lt;h1 className='work' onMouseDown={() =&gt; this.setState({grey: '1', van: true})}&gt;Vanstrings&lt;/h1&gt; &lt;/div&gt; &lt;/div&gt; {this.state.dash? &lt;div className='dashWrapper slide'&gt; &lt;h1 className='back'&gt;Back&lt;/h1&gt; &lt;/div&gt;: null} {this.state.busy? &lt;div/&gt;: null} {this.state.oke? &lt;div/&gt;: null} {this.state.scav? &lt;div/&gt;: null} {this.state.van? &lt;div/&gt;: null} &lt;/div&gt; &lt;/div&gt;</pre></div>

[英]Why won't the elements placed within my <div> show up?

暫無
暫無

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

相關問題 React 表單輸入不會讓我改變值 我正在嘗試插入文本,但本地終端上的輸入字段不允許我 reactJS 中的代碼不允許我顯示輸入值 為什么不能使用輸入更改值? 為什么我不能在 React 中更改我的輸入值? 為什么 Heroku 不允許我上傳我的網頁? 為什么我的Redux更改狀態根本不會更改? 為什么元素不能放在我的<div>出現?</div><div id="text_translate"><p> 就這么簡單:</p><pre> {this.state.dash? &lt;div className='dashWrapper slide'&gt; HELLO &lt;/div&gt;: null}</pre><p> 無論我在這個 div 中放什么,都沒有出現。 我真的很生氣,因為我已經做了很長時間了,我看不出有任何理由這不起作用。 div 本身顯示得非常好,但里面的任何東西都不是 go。</p><p> CSS:</p><pre> .dashWrapper { width: 100%; height: 100vh; background: white; }.slide { position: absolute; bottom: -100%; -webkit-animation: slide 1s forwards; animation: slide 1s forwards; -webkit-animation-delay: 1s; animation-delay: 1s; -webkit-animation-duration: 1.5s; animation-duration: 1.5s; } @-webkit-keyframes slide { 100% {bottom: 0;} } @keyframes slide { 100% {bottom: 0;} }</pre><p> 在我打破我的鍵盤之前請有人幫助我:(</p><p> 完整的 JSX</p><pre> &lt;div className='pageWrapper'&gt; &lt;div className='slides'&gt; &lt;div className='one' grey={this.state.grey}&gt; &lt;div className='chineseHeader'/&gt; &lt;div className='englishHeader'/&gt; &lt;div className='mePhoto'/&gt; &lt;div className='featuredWorkDivider'/&gt; &lt;div className='dash' onMouseDown={() =&gt; this.setState({grey: '1', dash: true})}/&gt; &lt;div className='allWorksDivider'/&gt; &lt;div className='allWorks'&gt; &lt;h1 className='work' onMouseDown={() =&gt; this.setState({grey: '1', busy: true})}&gt;Busy Map&lt;/h1&gt; &lt;h1 className='work' onMouseDown={() =&gt; this.setState({grey: '1', oke: true})}&gt;Oke&lt;/h1&gt; &lt;h1 className='work' onMouseDown={() =&gt; this.setState({grey: '1', scav: true})}&gt;Scav Hunt&lt;/h1&gt; &lt;h1 className='work' onMouseDown={() =&gt; this.setState({grey: '1', van: true})}&gt;Vanstrings&lt;/h1&gt; &lt;/div&gt; &lt;/div&gt; {this.state.dash? &lt;div className='dashWrapper slide'&gt; &lt;h1 className='back'&gt;Back&lt;/h1&gt; &lt;/div&gt;: null} {this.state.busy? &lt;div/&gt;: null} {this.state.oke? &lt;div/&gt;: null} {this.state.scav? &lt;div/&gt;: null} {this.state.van? &lt;div/&gt;: null} &lt;/div&gt; &lt;/div&gt;</pre></div> 為什么我在 React 中的受控輸入組件不會更新為 state? 為什么為輸入設置值在 Whatsapp 上不起作用?
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM