2010-09-28 11 views
5

tengo varias datepickers en una página ... todo parece estar funcionando bien, excepto que la parte altfield ...jQuery selector de fechas - altfield tema dinámico

es algo como esto posible ??

$(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(""); 
    }); 

}); 

Respuesta

9

Sí se puede hacer exactamente eso, sólo tiene que crear instancias de los selectores de fecha con un bucle .each(), así:

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

De esta manera dentro de la .each()this se refiere a lo que quiere, cada uno seleccionador de fecha individual, luego encontrando su campo alt. You can give it a try here.

+3

@Tom - Aquí está la demostración implementada con su código: http://jsfiddle.net/nick_craver/BhzE5/1/ –

+1

@NickCraver Genius! –

Cuestiones relacionadas