简体   繁体   English

为什么我的文本输入不允许我更改其中的值?

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

I defined the following input field in react:我在反应中定义了以下输入字段:

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>

I cannot change the value of item.quantity in the browser even though it is not specified as a read-only field.即使未将 item.quantity 指定为只读字段,我也无法在浏览器中更改它的值。

Data:数据:

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 refers to a fixed value within your object. item.quantity 是指 object 中的固定值。 Means you provide your input with a fixed value instead of a changeable state.意味着您为输入提供固定值,而不是可变的 state。 Here is the react way of creating a controlled input field https://reactjs.org/docs/forms.html这是创建受控输入字段https://reactjs.org/docs/forms.html的反应方式

In hooks that would be something like在钩子中,就像

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>
  )

}

And you map it as而你 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 表单输入不会让我改变值 - React form input won't let me change value 我正在尝试插入文本,但本地终端上的输入字段不允许我 - I'm trying to insert a text but the input field on my local terminal won't let me reactJS 中的代码不允许我显示输入值 - Code in reactJS doesn't allow me to show the value on the input 为什么不能使用输入更改值? - Why can't I change the value using my input? 为什么我不能在 React 中更改我的输入值? - Why can't I change my input value in React? 为什么 Heroku 不允许我上传我的网页? - Why Heroku doesn't allow me to upload my webpage? 为什么我的Redux更改状态根本不会更改? - Why won't my Redux change state change at all? 为什么元素不能放在我的<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? 为什么我在 React 中的受控输入组件不会更新为 state? - Why won't my controlled Input component in React update with the state? 为什么为输入设置值在 Whatsapp 上不起作用? - Why setting a value for an input won't work on Whatsapp?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM