2010-07-21 44 views
1

我被格式化jQuery UI日历的问题困住了。日历的格式基于用户偏好。如果用户选择从第一个选择框中的日期格式,那么UI日历格式,应根据该...请就如何实现这一想法..在此先感谢..问题格式化jQuery UI日历

jQuery代码

jQuery(function() { 

     var format = 'yy-mm-dd'; 
     jQuery('#dateFormat').change(function(){ 
     format = jQuery('#dateFormat').val(); 
     alert(format); 
     }); 

     jQuery('#fromDate').datepicker({ 
     showTime: false, 
     dateFormat: format, 
     showOn: 'button',buttonText: 'Date', buttonImage: 'images/calendar.gif', buttonImageOnly: true 
     }); 

     jQuery('#toDate').datepicker({ 
     showTime: false, 
     dateFormat: 'dd-mm-yy', 
     showOn: 'button',buttonText: 'Date', buttonImage: 'images/calendar.gif', buttonImageOnly: true 
     }); 

}); 

HTML代码如下

<label>Date Format</label> 
<select name="dateFormat" id="dateFormat"> 
<option val="1">dd-mm-yy</option> 
<option val="2">yy-mm-dd</option> 
</select> 
<label>Period start:</label> 
<input type="text" value="" size="30" name="fromDate" id="fromDate" /> 
<label>Period start:</label> 
<input type="text" value="" size="30" name="toDate" id="toDate" /> 

回答

2

试试这个:

var fromDate = jQuery('#fromDate'); 
var toDate = jQuery('#toDate'); 
var dateFormat = jQuery('#dateFormat'); 

dateFormat.change(function(){ 
    var format = jQuery(this).val(); 
    fromDate.datepicker('option','dateFormat',format); 
    toDate.datepicker('option','dateFormat',format); 
}); 

fromDate.datepicker({ 
    showTime: false, 
    dateFormat: format, 
    showOn: 'button',buttonText: 'Date', buttonImage: 'images/calendar.gif', buttonImageOnly: true 
}); 

toDate.datepicker({ 
    showTime: false, 
    dateFormat: 'dd-mm-yy', 
    showOn: 'button',buttonText: 'Date', buttonImage: 'images/calendar.gif', buttonImageOnly: true 
}); 
+0

非常感谢。它的工作很好,唯一的事情是,脚本对变化的反应非常缓慢.. :-) – Sullan 2010-07-21 07:48:29

+0

我编辑了代码并做了一些优化,现在就试试吧 – GerManson 2010-07-21 17:36:03