javascript - 我正在提交表单,但它没有进行第一次提交,它显示为空白。并且必需的属性也不起作用
问题描述
这是代码。
const Details = () => {
const [counter, setCounter] = useState(1)
查询状态用于获取表单的所有值,详细信息状态用于存储所有提交。
const [details, setDetails] = useState([])
const [ques, setQues] = useState({
question: "",
option1: "",
option2: "",
option3: "",
option4: "",
correct_option: ""
})
亨德尔在这里改变。
const handelChange = (e) => {
const name = e.target.name
const value = e.target.value
setQues({ ...ques, [name]: value })
}
我在这里提交表格:第一次输入的详细信息完全空白
const handelSubmit = (e) => {
e.preventDefault()
setDetails([...details, ques])
console.log(details)
setQues({ question: "", option1: "", option2: "", option3: "", option4: "", correct_option: "" })
}
return (
<div className="details-cont">
<div className="details">
表单:必填字段无效。
<form >
<div className="input_cont">
<label htmlFor="question">Question: {counter}</label>
<input required type="text" onChange={handelChange} value={ques.question} name="question" id="question" placeholder="Enter the question" />
</div>
<div className="input_cont">
<label htmlFor="option1">Option 1:</label>
<input type="text" required onChange={handelChange} value={ques.option1} name="option1" id="option1" placeholder="Enter the Option 1" />
</div>
<div className="input_cont">
<label htmlFor="option2">Option 2:</label>
<input type="text" required onChange={handelChange} value={ques.option2} name="option2" id="option2" placeholder="Enter the Option 2" />
</div>
<div className="input_cont">
<label htmlFor="option3">Option 3:</label>
<input type="text" required onChange={handelChange} value={ques.option3} name="option3" id="option3" placeholder="Enter the Option 3" />
</div>
<div className="input_cont">
<label htmlFor="option4">Option 4:</label>
<input type="text" required onChange={handelChange} value={ques.option4} name="option4" id="option4" placeholder="Enter the Option 4" />
</div>
<div className="toggle_cont">
<input className="correct_opt" required name="correct_option" type="number" onChange={handelChange} value={ques.correct_option} min="1" max="4" placeholder="correct option" />
<button onClick={handelSubmit} className="btn btn-add">Add</button>
</div>
</form>
<button className="btn btn-create">Create</button>
</div>
<div className="d-left">
Make your MCQ QUESTIONS on a GO!
</div>
</div >
)
}
解决方案
实际上,只要您通过<form>
标签提交表单,本机 html 验证就不会起作用。目前,您只是单击按钮提交。
你可以像这样改变它:
<form onSubmit={handleSubmit}>
{/* ... other fields ... */}
<div className="toggle_cont">
<input className="correct_opt" required name="correct_option" type="number" onChange={handelChange} value={ques.correct_option} min="1" max="4" placeholder="correct option" />
<button type="submit" className="btn btn-add">Add</button>
</div>
</form>
推荐阅读
- python - Python Rake 关键字
- laravel - Chrome 隐身模式下的 Laravel 419 问题
- r - 在 actionButton 闪亮 R 中嵌入具有多个参数的超链接
- java - 问题,Netbeans 没有显示任何文件夹
- python - 在python中计算相关矩阵的最有效方法
- xamarin - Android.OS.FileUriExposedException:通过 ClipData.Item.getUri()' 暴露在应用程序之外(这是不同的)
- sql - 在 Oracle SQL 中从 [x,y,z] 中提取值
- python - 在最短日期记录上合并两个 DF 并删除不匹配的日期行
- python-3.x - 在 Python 多处理中将 input() 与子进程一起使用
- flutter - 根据条件更新提供者侦听器