![](/img/trans.png)
[英]How to pass form user input through to the function which runs on form submit and create a cookie with the name of the users input?
[英]how to create a form which will only allow to submit if the input is not empty and is > 0?
我想在提交之前制定一个逻辑,说明输入值不应该是任何空值,并且应该大于 0。
const minValue = (input) => {
if(input < 0){
alert('input has to be greater than 0')
}else{
return input;
}
}
<Form className = "workout-form">
<div className ="form-row">
<label className ="form__label">Distance</label>
<input type = 'number' value = {distance} min = '0' onChange = {(e) => setDistance(minValue(e.target.value))} className ="form__input form__input--distance" placeholder="mi" autoFocus required/>
</div>
<button className ="form-btn" onClick = {submitWorkout}>Add Workout</button>
</Form>
如果此代码适用于检查负值,您应该能够将 if (input < 0) 更改为 if ((input < 0) || (input != ""))
const handleChange = e => { const value = parseInt(e.target.value); if(!isNaN(value) && value > 1){ setDistance(value) } }
onChange={handleChange}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.