javascript 使用 jquery-ui datepicker 在文本框中设置或显示日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7074029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
set or show date in textbox with jquery-ui datepicker
提问by bungdito
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.
我第一次在 datepicker 文本框中设置或显示日期时遇到问题 我有两个文本框,一个用于 datepicker,第二个用于备用格式。
<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"
then i want to set to the textbox.
i had try to make script like this:
我的问题是,如果我有这样的字符串:var datebirth="2011-08-16"
那么我想设置为文本框。
我曾尝试制作这样的脚本:
document.getElementById("birth_date_display").value = datebirth; //it's NOT works
document.getElementById("birth_date").value = datebirth; //it's works
thanks :)
谢谢 :)
回答by OverZealous
You want to use the setDate methodon 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);
回答by ShellyFM
You just need to call the setDateDatePicker method. 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.
您只需要调用setDateDatePicker 方法。这将设置 DatePicker 的值并使用您为 dateFormat 和 altFormat 指定的格式的适当值更新两个文本框元素。
var datebirth="2011-08-16";
$("#birth_date_display").datepicker("setDate",new Date(datebirth));
回答by JennyJane
You may also try to format month names instead of 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));