我们在我的工作场所使用 jquery UI 1.8.23。
如何捕获在日期选择器中单击的日期,以便将当前日期格式化为日期戳并将该值传递到隐藏的表单字段中?
这是我尝试过的一些代码:
$('.ui-state-default').live('click', function(){
console.log('date clicked');
var currentdate = $(this).text();
var d = currentdate;
var m = monthFormat($('.ui-datepicker-month').text());
var y = $('.ui-datepicker-year').text();
$('a.ui-state-default').removeClass('ui-state-highlight');
$(this).addClass('ui-state-highlight');
if (currentdate.length < 2) {
d = "0" + currentdate;
}
var datestamp = y + "-" + m + "-" + d;
console.log(datestamp);
$('#dateHidden').val(datestamp);
});
感谢您查看我的代码,我感谢你们提供的任何帮助。
最佳答案
您无需执行任何操作即可设置日期格式。 DatePicker
小部件会为您完成此操作。
只需使用 altField
和 altFormat
属性:
$("#myDatePicker").datepicker({
// The hidden field to receive the date
altField: "#dateHidden",
// The format you want
altFormat: "yy-mm-dd",
// The format the user actually sees
dateFormat: "dd/mm/yy",
onSelect: function (date) {
// Your CSS changes, just in case you still need them
$('a.ui-state-default').removeClass('ui-state-highlight');
$(this).addClass('ui-state-highlight');
}
});
文档:
http://api.jqueryui.com/datepicker/#option-altField
http://api.jqueryui.com/datepicker/#option-altFormat
演示:
关于jQuery UI datepicker - 尝试从点击日期捕获点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18640143/