谢谢你们,
抱歉更新晚了。
我已经使用以下代码进行了此操作。
正如@adeneo 指出的那样,我正在使用 C# 和 Asp.Net。
我正在从后面的 C# 代码创建两个动态文本框,并将 javascript 函数添加到文本框的 onclik 事件中,如下所示:
DeliveryDateTextBox.Attributes.Add("onClick", "javascript:CalendarPopUpOnClick('" + DeliveryDateTextBox.ClientID + "','" + Hiddenfield_DeliveryDateTextBox.ClientID + "');");
DueDateTextBox.Attributes.Add("onClick", "javascript:CalendarPopUpOnClick('" + DueDateTextBox.ClientID + "','" + Hiddenfield_DueDateTextBox.ClientID + "');");
并在 Javascript 文件中添加以下代码:
$(document).ready(function()
{
/* Get id prefixes for checkboxes, checkbox control elements */
var exampleId = $("input[use='proprietary']").attr("id");
var serverIdPrefixEndPos = exampleId.indexOf("exampleID");
serverIdPrefix = exampleId.substring(0,serverIdPrefixEndPos);
/* Datepicker BEGIN */
setTimeSpanBeginInput = function(){
var dateTime = $('#'+ serverIdPrefix +'timeSpanFromDeliveryDateHiddenField').val();
var dateTimeArr = dateTime.split(" ");
var dateOnly = dateTimeArr[0];
$('#'+ serverIdPrefix +'timeSpanFromDeliveryDate').val(dateOnly);
} //when calendar closed, copy hidden field value to main date field
$('#'+ serverIdPrefix +'timeSpanFromDeliveryDate').datepicker({dateFormat: 'yy-mm-dd', onSelect: function(dateText){ $('#'+ serverIdPrefix +'timeSpanFromDeliveryDateHiddenField').val(dateText + ' 12:00:00 PM'); }, onClose: setTimeSpanBeginInput});
setTimeSpanEndInput = function(){
var dateTime = $('#'+ serverIdPrefix +'timeSpanToDateHiddenField').val();
var dateTimeArr = dateTime.split(" ");
var dateOnly = dateTimeArr[0];
$('#'+ serverIdPrefix +'timeSpanToDeliveryDate').val(dateOnly);
} //when calendar closed, copy hidden field value to main date field
$('#'+ serverIdPrefix +'timeSpanToDeliveryDate').datepicker({dateFormat: 'yy-mm-dd', onSelect: function(dateText){ $('#'+ serverIdPrefix +'timeSpanToDateHiddenField').val(dateText + ' 12:00:00 PM'); }, onClose: setTimeSpanEndInput});
/* Datepicker END */
});//end document ready
function CalendarPopUpOnClick(TextBoxId, HiddenFieldId)
{
if (TextBoxId) {
var textBox = $('#' + serverIdPrefix + TextBoxId),
hiddenField = $('#' + serverIdPrefix + HiddenFieldId);
textBox.focus();
$(":input").bind('paste', function(e) {
setTimeout(function() {
var dateval =hiddenField.val();
if(dateval.indexOf('0001-01-01')==-1)
textBox.val( hiddenField.val().split(" ")[0] );
else
textBox.val('');
}, 100);
});
textBox.keypress(function(event) {event.preventDefault();});
hiddenField.keypress(function(event) {event.preventDefault();});
var dateTime = hiddenField.val();
var dateTimeArr = dateTime.split(" ");
var dateOnly = dateTimeArr[0];
textBox.datepicker({
dateFormat: 'yy-mm-dd',
onSelect: function (dateOnly) {
hiddenField.val(dateOnly + ' 12:00:00 PM');
},
onClose: function() {
var dateval =hiddenField.val();
if(dateval.indexOf('0001-01-01')==-1)
textBox.val( hiddenField.val().split(" ")[0] );
}
});
textBox.datepicker().datepicker('show');
}
}