我当前的代码是从用户那里获得3个日期并将其显示给用户,如
2013-2-23
2013-2-22
2013-1-19
但是我希望它比\n
更多。我希望它能够像
start 2013-2-23
middle 2013-2-22
end 2014-2-24
我如何更改此代码?
$( "#selectdate" ).datepicker({
//altField: "#comment",
numberOfMonths:1,
dateFormat: "yy mm dd",
altFormat: "dd mm yy",
minDate: -0,
onSelect: function( selectedDate ) {
$("#selectdate").val('');
if($('#comment').val()!='') {
$('#comment').val($('#comment').val()+'\n'+selectedDate);
} else {
$('#comment').val(selectedDate);
}
}
});
答案 0 :(得分:1)
将这些标签放在数组中,并使用递增索引访问标签。
var counter = 0; //incrementing index
var label = ['start', 'middle', 'end']; //labels
$( "#selectdate" ).datepicker({
//altField: "#comment",
numberOfMonths:1,
dateFormat: "yy mm dd",
altFormat: "dd mm yy",
minDate: -0,
onSelect: function( selectedDate ) {
if(counter==3) counter = 0; //to avoid accessing non-existent array index go back to 0
$("#selectdate").val('');
if($('#comment').val()!='') {
$('#comment').val($('#comment').val()+'\n'+ label[counter] + ' ' + selectedDate);
} else {
$('#comment').val(label[counter] + ' ' + selectedDate);
}
counter++; //increment the label index
}
});