如何限制在react.js或html输入类型中选定的启动日期之后开始日期之前的结束日期= date; quot;
function App() {
let [account, setAccount] = React.useState({
startdate: "",
enddate: "",
reason: "",
leavetype: "",
});
function handleChange(e) {
let name = e.target.name;
let value = e.target.value;
account[name] = value;
setAccount(account);
}
function Submit(e) {
e.preventDefault();
console.log(account);
}
return (
<div>
<fieldset>
<form className="" method="post" onSubmit={Submit}>
Start Date:
<input
type="date"
name="startdate"
min={new Date().toISOString().split("T")[0]}
onChange={handleChange}
required
/>
End Date:
<input
type="date"
name="enddate"
// min={}
onChange={handleChange}
required
/>
</form>
</fieldset>
</div>
);
}
ReactDOM.render(<App/>, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="root"></div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用所拥有的状态。
当您查看代码时,您还应考虑其他一些更改。
只要没有选择的启动日期或设置开始结束日期的明智默认值(例如,今天两者都设置这些默认值),我将 disable 。
更重要的是:您应该将状态视为不变的,这意味着您需要每当国家随着反应而变化时都需要创建一个新对象。否则该组件将不会重新渲染。因此,请使用
setAccount({...帐户,[名称]:value});
更新状态。您需要在此处使用[]
,因此变量内容被解释为属性的名称。参见此线程。您可能还想使用
&lt; label&gt;
,也许是&lt; legend&gt;
。请参阅&gt; field;代码>
。
有关控制datepicker的更多选项,请参见” noreferrer“> <代码>&lt;输入类型=“ date”&gt; 。
You need to use the state that you have.
When you look at the code there are some other changes which you should consider.
I would disable the end date date picker as long as there is no start date selected or set sensible default values for start end end date (e.g. today for both) and set those when defining the state.
More important: You should treat state as immutable that means you need to create a new object every time the state changes as React only does a shallow comparison. Otherwise the component won't re-render. So use
setAccount({...account, [name]: value});
to update your state. You need to use[]
here so the variable content is interpreted as the name of the property. See this thread.You also probably want to use
<label>
and maybe<legend>
. See the MDN docs for<fieldset>
.For more options to control the datepicker see MDN docs for
<input type="date">
.