【发布时间】:2018-10-13 07:04:21
【问题描述】:
我刚开始使用 React,一直停留在编辑表单上。我需要预填充输入,还需要能够对其进行编辑。如果我输入 defaultValue,我不能在输入元素上输入值。此时我可以输入输入,但我必须编辑两个字段(表单有两个字段)才能保存这两个值。我应该怎么做?
示例: 我有食谱名称和成分,我用编辑表单打开模式,它是预填充的,我添加了一些成分但保持名称不变。它节省了:
{name: "", ingredients: ing1,ing2,newIngredient}
<form role="form">
<div className="form-group">
<label htmlFor={props.recipeName}>Recipe</label>
<input
type="text"
className="form-control"
defaultValue={props.recipeName || ''}
name="recipeName"
placeholder="Recipe name"
onChange={props.handleChange.bind(this)}/>
</div>
<div className="form-group">
<label htmlFor={props.recipeIngredients}>Ingredients</label>
<textarea
type="text"
className="form-control"
defaultValue={props.recipeIngredients || ''}
placeholder="Enter ingredients separated by commas..."
onChange={props.handleChange.bind(this)}
name="recipeIngredients"></textarea>
</div>
</form>
【问题讨论】:
-
嘿,你能看看这个问题吗?它与此非常相似,因此您可能知道如何提供帮助。谢谢stackoverflow.com/questions/58008167/…
标签: javascript forms reactjs jsx