如果没有选择日期,您如何防止引导日期提示符从输入中清除现有值? [英] How do you prevent bootstrap-datepicker from clearing existing value from input if no date is selected?

查看:157
本文介绍了如果没有选择日期,您如何防止引导日期提示符从输入中清除现有值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户的出生日期预先填充了php的div:

I have have a div with a user's date of birth pre-populated with php:

<div id="datepicker" class="input-group date">
    <label for="dob">Date of Birth</label> <input type="text" class="form-control" id="dob" name="dob" value="<?php $date = new DateTime($this->swimmers->dob); echo $date->format('d/m/Y');?>"><span class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span>
</div>

javascript:

The javascript:

<script>     
    $('.container .input-group.date').datepicker({
    startDate: "01/01/1900",
    endDate: "01/01/2100",
    format: "dd/mm/yyyy",
    autoclose: true,
    todayHighlight: true
    });
</script>

引导设置并正常工作,但是当dob输入获取并失去焦点,日期选择它清除预先填充的值。

Bootstrap is setup and working correctly however, when the "dob" input gets and loses focus without making a date selection it clears the value that was pre-populated.

但是,如果通过datepicker选择了日期,然后输入获取并失去焦点,则不会发生

This however doesn't happen if a date has been selected via the datepicker and then the input gets and loses focus.

如果有人点击,预填充值如何保留在该字段中?

How can the pre-populated value be kept in the field if someone clicks in it?

编辑:您第一次进行日期选择时,会清除预先填充的值,但不会插入所选日期,直到您再次进行日期选择为止。

The first time you make a date selection, it clears the pre-populated value but doesn't insert the selected date until you make a date selection a second time.

更新:工作代码更新!

推荐答案

无法确定没有更多的例子,但问题可能是 $ this-> swimmers-> dob 您输入的字符串不会被bootstrap-datepicker识别为日期格式。您可以通过在输入中添加 data-date-format 属性来定义其使用的日期格式。

Can't tell for sure without more of an example but the problem is probably that the $this->swimmers->dob string you are giving to the input is not recognised as a date format by bootstrap-datepicker. You can define the date format it uses by adding the data-date-format attribute to your input.

例如,如果您使用MySQL格式的日期或PHP的DateTime对象,其中可以使用 $ date->格式('Ym-d')

For example if you were using a MySQL formatted date or PHP's DateTime object with $date->format('Y-m-d') you could use:

<div id="datepicker" class="input-group date">
    <input type="text" class="form-control" id="dob" name="dob" value="<?php echo $this->swimmers->dob;?>" data-date-format="yy-mm-dd"><span class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span>
</div>

这篇关于如果没有选择日期,您如何防止引导日期提示符从输入中清除现有值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆