简体   繁体   English

使用 jquery-ui datepicker 在文本框中设置或显示日期

[英]set or show date in textbox with jquery-ui datepicker

I have problem to set or show date in datepicker textbox at firsttime I have two textboxes, the one is for datepicker and the second is for alternate format.我第一次在日期选择器文本框中设置或显示日期时遇到问题我有两个文本框,一个用于日期选择器,第二个用于替代格式。

<input type="text" id="birth_date_display" />
<input type="text" id="birth_date" />

and scripts like this和这样的脚本

$( "#birth_date_display" ).datepicker({
 dateFormat: "d M, yy",
 altField: "#birth_date",
 altFormat: "yy-mm-dd"
});

my question is, How if i have String like: var datebirth="2011-08-16"我的问题是,如果我有这样的字符串: var datebirth="2011-08-16"
then i want to set to the textbox.然后我想设置到文本框。
i had try to make script like this:我曾尝试制作这样的脚本:

document.getElementById("birth_date_display").value = datebirth; //it's NOT works
document.getElementById("birth_date").value         = datebirth; //it's works

thanks:)谢谢:)

You want to use the setDate method on the DatePicker:您想在 DatePicker 上使用setDate 方法

$( "#birth_date_display" ).datepicker("setDate", datebirth);
$( "#birth_date" ).datepicker("setDate", datebirth);

Edit: You could probably also do it this way, but I haven't tested it:编辑:你也可以这样做,但我还没有测试过:

$( "#birth_date_display, #birth_date" ).datepicker("setDate", datebirth);

You just need to call the setDate DatePicker method.您只需要调用setDate DatePicker 方法。 This will set the value of the DatePicker and update both text box elements with the appropriate value in the formats you specified for the dateFormat and altFormat.这将设置 DatePicker 的值,并以您为 dateFormat 和 altFormat 指定的格式使用适当的值更新两个文本框元素。

var datebirth="2011-08-16"; 
$("#birth_date_display").datepicker("setDate",new Date(datebirth));

You may also try to format month names instead of integer您也可以尝试格式化月份名称而不是 integer

var Month = "Feb" //some month; 
var Year = "2013" //some year; 
var mydate = new Date('1 '+ Month + " " + Year);

$(".txtDate").datepicker('setDate', new Date(Year, mydate.getMonth(), 1)); $(".txtDate").datepicker('setDate', new Date(Year, mydate.getMonth(), 1));

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

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