【发布时间】:2011-06-03 05:11:26
【问题描述】:
我有一个日期选择器输入,其中填充了页面加载时的今天日期。使用 datepicker 时,将日期拆分为月、日和年,并放入三个隐藏字段,这些字段与 post 数据一起发送到服务器。当用户决定今天的日期很好并且不使用日期选择器时,问题就出现了;如何将日期片段放入这三个隐藏字段?
我尝试将今天的日期分解为多个片段并将其添加到这些字段中,但我尝试将其添加到 jQuery 或使用纯 JavaScript 的所有方式都会破坏整个过程。这是我想要构建的工作代码:
jQuery
$(function() {
//set your date picker
$("#CI").datepicker({
dateFormat: 'mm/dd/yy',
changeMonth: true,
changeYear: true,
showOn: "both",
buttonImage: "css/images/calendar-green.gif",
buttonImageOnly: true,
buttonImageText: "Calendar",
beforeShow: function(dateText, instance) {
$("#CI").datepicker('setDate', new Date());
},
onSelect: function(dateText, instance) {
var pieces = dateText.split("/");
$("#CID").val(pieces[0]);
$("#CIM").val(pieces[1]);
$("#CIY").val(pieces[2]);
}
})
//get the current date
var todaysDate = new Date();
//format the date in the right format (add 1 to month because JavaScript counts from 0)
formatDate = (todaysDate.getMonth() + 1) + '/' +
todaysDate.getDate() + '/' +
todaysDate.getFullYear();
//set the input value
$('#CI').val(formatDate);
});
HTML
<input type='text' id='CI' name="CI">
<input type="text" name="CID" id="CID" />
<input type="text" name="CIM" id="CIM" />
<input type="text" name="CIY" id="CIY" />
【问题讨论】:
标签: jquery datepicker