JS输入类型日期字段,保留表单提交后选择的值 [英] JS input type date field, keep values selected after form submission
本文介绍了JS输入类型日期字段,保留表单提交后选择的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法从
Is there a way for the
<input type="date" id="from" class="mdl-textfield__input" name="from" value="{{moment date=d format='YYYY-MM-DD'}}" style="position:relative; float: left; width: 180px; bottom: 10px;"/>
字段在表单提交后记住选定的值?
field to remember selected value after form submission?
推荐答案
您可以使用HTML5本地存储在提交后保存日期。
您可以这样做:
You can use the HTML5 Local Storage to save the date after the submit. This is how you can do it:
1)在您的表单中添加onsubmit函数:
1) Add to your form onsubmit function:
<form onsubmit="setDate()">
2)创建Setter和Getter函数到日期:
2) Create Setter and Getter functions to the date:
function setDate(){
var value = document.getElementById('from').value;
localStorage.setItem("user_selected_date", value);
}
function getDate(){
if (localStorage.getItem("user_selected_date") === null) {// Check if there is selected date.
return "{{moment date=d format='YYYY-MM-DD'}}";
}
return localStorage.getItem("user_selected_date");
}
3)将值设置为您的输入:
3) Set the value to your input:
document.getElementById('from').value = getDate();
这篇关于JS输入类型日期字段,保留表单提交后选择的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文