2010-09-28 93 views
5

我在页面上有多个日期选择器......除了我的altfield部分,我似乎都能正常工作......jquery datepicker - 动态altfield问题

是这样的可能吗?

$(document).ready(function(){ 

    $(".datepicker").datepicker({ 
     beforeShowDay: function(date){ return [(date.getDay() == 1 || date.getDay() == 4), ""] }, 
     showOn: "button", 
     buttonText: "Book now...", 
     showWeek: true, 
     firstDay: 1, 
     onSelect: function(date) { 
      $(this).parent().find('.selecteddate').prepend("Date: " + $(this).parent().find('.datepicker').val() + " - <a href='javascript:;' class='cleardate'>clear date</a>"); 
      $(this).parent().find('button').hide(); 
     }, 
     dateFormat: "DD, d MM, yy", 

     altField: $(this).closest('div').find('.datepickeralt'), 

     altFormat: "dd/mm/yy", 
     navigationAsDateFormat: true, 
     minDate: 0, 
    }); 

    $(".cleardate").live('click',function(){ 
     $(this).closest('div').find('.datepicker').datepicker("setDate", null); 
     $(this).closest('div').find('.datepickeralt').val(""); 
     $(this).closest('div').find('button').show(); 
     $(this).parent().html(""); 
    }); 

}); 

回答

9

是的,你可以做到这些,你只需要一个.each()环实例的日期选择器,像这样:

$('.datepicker').each(function() { 
    $(this).datepicker({ 
    altField: $(this).closest('div').find('.datepickeralt') 
    }); 
}); 

.each()this内这种方式指的是你想要什么,每个别日期选择器,然后找到 alt字段。 You can give it a try here

+3

@Tom - 下面是你的代码实现的演示:http://jsfiddle.net/nick_craver/BhzE5/1/ – 2010-09-28 10:53:14

+1

@NickCraver天才! – 2012-12-05 18:43:47