The above code has some error.
Here is my final solution: write another plugin to get the date.
HTML:
div id=dateRange
Plan Date: input type=text id=eventFromDate/ to input
type=text id=eventToDate/ br/
Actual Date: input type=text id=actualFromDate/ to input
type=text id=actualToDate/
/div
JS:
$(#dateRange input).datepickerRange({showOn:'button'});
/* Update datepicker if found and return the date */
$.fn.getInputDate = function(){
var elem = this[0];
if(elem){
var inst = $.datepicker._getInst(elem._calId);
if(inst){
inst._setDateFromField(elem);
return inst._getDate();
}
}
return null;
}
/*
Assume from/to date have same ID prefix
options can provide suffix
options's beforeShow is overriden
*/
$.fn.datepickerRange = function(options){
options = options || {};
var fromSuffix = options.fromSuffix || FromDate;
var toSuffix = options.toSuffix || ToDate;
options.beforeShow = function(input){
var isFrom = (new RegExp(fromSuffix+$)).test(input.id);
var prefix = input.id.replace(new
RegExp((+fromSuffix+|+toSuffix
+)$),);
return {
minDate: isFrom? null:
$(#+prefix+fromSuffix).getInputDate(),
maxDate: !isFrom? null:
$(#+prefix+toSuffix).getInputDate()
};
}
return this.datepicker(options);
}
On 4月11日, 上午3時14分, Jacky See [EMAIL PROTECTED] wrote:
I have found some wicked way to do it.
This is the code where I'm writing a plugin to accept date range pair
and auto-init them.
//Assuming from/to date have same prefix id (e.g. #eventFromDate,
#eventToDate)
$.fn.datepickerPair = function(options){
return this.datepicker(
$.extend({
beforeShow:function(elem){
var id = elem.id;
var isFrom = /FromDate$/.test(id);
var prefix = id.replace(/(FromDate|ToDate)$/,'');
var minDate = isFrom?null: $.datepicker._getInst($
(#+prefix+FromDate).get(0)._calId)._setDateFromField(#+prefix
+FromDate);
var maxDate = !isFrom?null: $.datepicker._getInst($
(#+prefix+ToDate).get(0)._calId)._setDateFromField(#+prefix
+ToDate);
return {minDate:minDate, maxDate:maxDate};
},options)
);
}
Any other 'cleaner' way?
On 4月11日, 上午1時09分, Jacky See [EMAIL PROTECTED] wrote:
Dear all,
This is about the ui datepicker.
I have an input field with a datepicker, using image as trigger.
The field is not read only, user are allowed to input by keyboard.
The problem is that when user type some invalid input like '33'
the $('#input').datepicker('getdate'), will still only get the last
selected date.
Is that any way to get the 'fresh' date?
Any trigger I can call?
I need it to fill in a date range using 'beforeShow' option.