简体   繁体   English

在Jquery UI datepicker字段中从数据库预过滤日期

[英]Prefil date from database in Jquery UI datepicker field

I have an edit form, where the values are populated from the database. 我有一个编辑表单,其中的值是从数据库中填充的。 I have one date field in the format yyyy-mm-dd . 我有一个date字段,格式为yyyy-mm-dd I have setup the jQuery Datepicker and its working on the date field, however, when the form first loads, the date field is empty even though the input type has a value from the database, when I checked the HTML source. 我已经设置了jQuery Datepicker及其在date字段上的工作,但是,当我第一次检查HTML源代码时,当表单第一次加载时,即使输入类型具有数据库中的值,date字段也为空。

How can I make my date field display the date from the database? 如何使我的日期字段显示数据库中的日期?

EDIT: 编辑:

I want my input values to be visible in the text field rather than grabbing the values from database and again inserting it into a jQuery/javascript variable/function(default date, showbefore what not). 我希望我的输入值在文本字段中可见,而不是从数据库中获取值,然后再次将其插入到jQuery / javascript变量/函数中(默认日期,否则显示)。

What can I do to disable datepicker from hiding my values in the first place? 我该怎么做才能禁止datepicker首先隐藏我的值? and only replace the values if I click and choose a date. 并且仅当我单击并选择一个日期时才替换值。

HTML Code: HTML代码:

<input type="text" id="date" name="reg_date" value="2012-01-30" />

The actual code is in PHP where the value is being retrieved from database and inserted in the value field. 实际的代码在PHP中,其中从数据库中检索值并将其插入到value字段中。 The values are correctly generated and inserted because I verified it by looking at the HTML source in firefox. 这些值已正确生成和插入,因为我通过查看firefox中的HTML源进行了验证。

Datepicker setting: 日期选择器设置:

$(function() {
    $( "#date" ).datepicker({
        changeMonth: true,
        changeYear: true
});
    $( "#date" ).datepicker({ dateFormat: "yy-mm-dd" });
    $( "#date" ).datepicker( "option", "dateFormat", "yy-mm-dd" );
});

Datepicker is over-writing value. Datepicker是覆盖值。 I would suggest you trigger datepicker when focused on date field. 我建议您在关注日期字段时触发datepicker。

You need to set the default date in the datepicker settings code. 您需要在日期选择器设置代码中设置默认日期。

Example: $( ".selector" ).datepicker({ defaultDate: +7 }); 示例: $( ".selector" ).datepicker({ defaultDate: +7 }); to set the default date as 7 days from now. 将默认日期设置为从现在开始的7天。

True story. 真实的故事。

change for this: 为此:

$(function() {
    $( ".date" ).datepicker({
        dateFormat: "yy-mm-dd",                 
        changeMonth: true,
        changeYear: true
    });
});

if your browser has cached ... press CTRL + F5 如果您的浏览器已缓存...请按CTRL + F5

Well, now you have posted your code it is very obvious. 好了,现在您已经发布了代码,这非常明显。

Your database date format is YYYY-MM-DD Your jQuery UI format is yy-mm-dd See what's wrong? 您的数据库日期格式为YYYY-MM-DD您的jQuery UI格式为yy-mm-dd看看出了什么问题? Your missing at least 2 digits of the year. 您一年中至少缺少2位数字。

To fix this, simply enter the correct format in your code. 要解决此问题,只需在代码中输入正确的格式。

Example: 例:

$( "#date" ).datepicker({ dateFormat: "dd-MM-yyyy" });
$( "#date" ).datepicker( "option", "dateFormat", "dd-MM-yyyy" );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM