我希望datetimepicker的值在页面刷新时保持不变,而不是重置为默认值。值(:deadline)存储在params哈希中,我想在页面刷新时将datetimepicker的值设置为params [:deadline],或者在表单中提交不正确的信息。
可以这样做或者我应该使用会话吗?
以下是我尝试解决此问题的方法:
<script type="text/javascript">
$(function () {
$('#datetimepicker1').datetimepicker({
format: 'YYYY-MM-DD HH:mm',
minDate: moment().add(1, 'd').toDate(),
if(#{params.has_key?(:deadline)}){ // What I tried
$("#datetimepicker1").value("2018-06-20");
}
});
});
</script>
<%= f.label :deadline %>
<div class='input-group date' id='datetimepicker1'>
<%= f.text_field :deadline, class: "form-control" %>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
答案 0 :(得分:0)
您可以根据deadline
属性为输入添加值,并将minDate
更改为defaultDate
:
<%= f.label :deadline %>
<div class='input-group date' id='datetimepicker1'>
<%= f.text_field :deadline, class: "form-control", value: @my_object.deadline %>
</div>
<script>
$(function () {
$('#datetimepicker1').datetimepicker({
format: 'YYYY-MM-DD HH:mm',
useCurrent: false,
minDate: moment().add(1, 'd').toDate()
});
});
</script>