Estoy trabajando con el selector de fecha de arranque de esta fuente: http://eonasdan.github.io/bootstrap- datetimepicker /

Ya tengo un selector de fechas casi como quiero ser, un selector en línea del que necesito detectar cambios (en vivo). Se parece a esto:

enter image description here

Se genera con este Código:

<div class="row">
    <div class="col-md-6">
        <div id="start_datepicker"></div>
    </div>
    <div class="col-md-6">
        <div id="start_timepicker"></div>
    </div>
</div>

Y el Javascript que lo llama:

$('#start_datepicker').datetimepicker(
{
    format: "D-M-Y",
    locale: "de",
    inline: true,
    calendarWeeks: true
});

$('#start_timepicker').datetimepicker(
{
    format: "HHmm",
    stepping: 15,
    locale: "de",
    inline: true
});

Esto funciona bien y genera un código similar al siguiente:

<div id="start_datepicker" style="">
    <div class="bootstrap-datetimepicker-widget usetwentyfour" style="display: block;">
        <ul class="list-unstyled">
            <li>
                <div class="datepicker">
                    <div class="datepicker-days" style="display: block;">
                        <table class="table-condensed">
                            <thead>
                                <tr>
                                    <th class="prev" data-action="previous"><span class="glyphicon glyphicon-chevron-left" title=
                                    "Previous Month"></span></th>
                                    <th class="picker-switch" colspan="6" data-action="pickerSwitch" title="Select Month">Mai 2016</th>
                                    <th class="next" data-action="next"><span class="glyphicon glyphicon-chevron-right" title="Next Month"></span></th>
                                </tr>
                                <tr>
                                    <th class="cw">#</th>
                                    <th class="dow">Mo</th>
                                    <th class="dow">Di</th>
                                    <th class="dow">Mi</th>
                                    <th class="dow">Do</th>
                                    <th class="dow">Fr</th>
                                    <th class="dow">Sa</th>
                                    <th class="dow">So</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td class="cw">17</td>
                                    <td class="day old" data-action="selectDay" data-day="25.04.2016">25</td>
                                    <td class="day old" data-action="selectDay" data-day="26.04.2016">26</td>
                                    <td class="day old" data-action="selectDay" data-day="27.04.2016">27</td>
                                    <td class="day old" data-action="selectDay" data-day="28.04.2016">28</td>
                                    <td class="day old" data-action="selectDay" data-day="29.04.2016">29</td>
                                    <td class="day old weekend" data-action="selectDay" data-day="30.04.2016">30</td>
                                    <td class="day weekend" data-action="selectDay" data-day="01.05.2016">1</td>
                                </tr>
                                <tr>
                                    <td class="cw">18</td>
                                    <td class="day" data-action="selectDay" data-day="02.05.2016">2</td>
                                    <td class="day" data-action="selectDay" data-day="03.05.2016">3</td>
                                    <td class="day" data-action="selectDay" data-day="04.05.2016">4</td>
                                    <td class="day" data-action="selectDay" data-day="05.05.2016">5</td>
                                    <td class="day" data-action="selectDay" data-day="06.05.2016">6</td>
                                    <td class="day weekend" data-action="selectDay" data-day="07.05.2016">7</td>
                                    <td class="day weekend" data-action="selectDay" data-day="08.05.2016">8</td>
                                </tr>
                                <tr>
                                    <td class="cw">19</td>
                                    <td class="day" data-action="selectDay" data-day="09.05.2016">9</td>
                                    <td class="day active today" data-action="selectDay" data-day="10.05.2016">10</td>
                                    <td class="day" data-action="selectDay" data-day="11.05.2016">11</td>
                                    <td class="day" data-action="selectDay" data-day="12.05.2016">12</td>
                                    <td class="day" data-action="selectDay" data-day="13.05.2016">13</td>
                                    <td class="day weekend" data-action="selectDay" data-day="14.05.2016">14</td>
                                    <td class="day weekend" data-action="selectDay" data-day="15.05.2016">15</td>
                                </tr>
                                <tr>
                                    <td class="cw">20</td>
                                    <td class="day" data-action="selectDay" data-day="16.05.2016">16</td>
                                    <td class="day" data-action="selectDay" data-day="17.05.2016">17</td>
                                    <td class="day" data-action="selectDay" data-day="18.05.2016">18</td>
                                    <td class="day" data-action="selectDay" data-day="19.05.2016">19</td>
                                    <td class="day" data-action="selectDay" data-day="20.05.2016">20</td>
                                    <td class="day weekend" data-action="selectDay" data-day="21.05.2016">21</td>
                                    <td class="day weekend" data-action="selectDay" data-day="22.05.2016">22</td>
                                </tr>
                                <tr>
                                    <td class="cw">21</td>
                                    <td class="day" data-action="selectDay" data-day="23.05.2016">23</td>
                                    <td class="day" data-action="selectDay" data-day="24.05.2016">24</td>
                                    <td class="day" data-action="selectDay" data-day="25.05.2016">25</td>
                                    <td class="day" data-action="selectDay" data-day="26.05.2016">26</td>
                                    <td class="day" data-action="selectDay" data-day="27.05.2016">27</td>
                                    <td class="day weekend" data-action="selectDay" data-day="28.05.2016">28</td>
                                    <td class="day weekend" data-action="selectDay" data-day="29.05.2016">29</td>
                                </tr>
                                <tr>
                                    <td class="cw">22</td>
                                    <td class="day" data-action="selectDay" data-day="30.05.2016">30</td>
                                    <td class="day" data-action="selectDay" data-day="31.05.2016">31</td>
                                    <td class="day new" data-action="selectDay" data-day="01.06.2016">1</td>
                                    <td class="day new" data-action="selectDay" data-day="02.06.2016">2</td>
                                    <td class="day new" data-action="selectDay" data-day="03.06.2016">3</td>
                                    <td class="day new weekend" data-action="selectDay" data-day="04.06.2016">4</td>
                                    <td class="day new weekend" data-action="selectDay" data-day="05.06.2016">5</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <div class="datepicker-months" style="display: none;">
                        <table class="table-condensed">
                            <thead>
                                <tr>
                                    <th class="prev" data-action="previous"><span class="glyphicon glyphicon-chevron-left" title=
                                    "Previous Year"></span></th>
                                    <th class="picker-switch" colspan="6" data-action="pickerSwitch" title="Select Year">2016</th>
                                    <th class="next" data-action="next"><span class="glyphicon glyphicon-chevron-right" title="Next Year"></span></th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td colspan="8"><span class="month" data-action="selectMonth">Jan.</span><span class="month" data-action=
                                    "selectMonth">Febr.</span><span class="month" data-action="selectMonth">Mrz.</span><span class="month" data-action=
                                    "selectMonth">Apr.</span><span class="month active" data-action="selectMonth">Mai</span><span class="month"
                                    data-action="selectMonth">Jun.</span><span class="month" data-action="selectMonth">Jul.</span><span class="month"
                                    data-action="selectMonth">Aug.</span><span class="month" data-action="selectMonth">Sept.</span><span class="month"
                                    data-action="selectMonth">Okt.</span><span class="month" data-action="selectMonth">Nov.</span><span class="month"
                                    data-action="selectMonth">Dez.</span></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <div class="datepicker-years" style="display: none;">
                        <table class="table-condensed">
                            <thead>
                                <tr>
                                    <th class="prev" data-action="previous"><span class="glyphicon glyphicon-chevron-left" title="Next Decade"></span></th>
                                    <th class="picker-switch" colspan="6" data-action="pickerSwitch" title="Select Decade">2011-2022</th>
                                    <th class="next" data-action="next"><span class="glyphicon glyphicon-chevron-right" title=
                                    "Previous Decade"></span></th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td colspan="8"><span class="year" data-action="selectYear">2011</span><span class="year" data-action=
                                    "selectYear">2012</span><span class="year" data-action="selectYear">2013</span><span class="year" data-action=
                                    "selectYear">2014</span><span class="year" data-action="selectYear">2015</span><span class="year active" data-action=
                                    "selectYear">2016</span><span class="year" data-action="selectYear">2017</span><span class="year" data-action=
                                    "selectYear">2018</span><span class="year" data-action="selectYear">2019</span><span class="year" data-action=
                                    "selectYear">2020</span><span class="year" data-action="selectYear">2021</span><span class="year" data-action=
                                    "selectYear">2022</span></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <div class="datepicker-decades" style="display: none;">
                        <table class="table-condensed">
                            <thead>
                                <tr>
                                    <th class="prev" data-action="previous"><span class="glyphicon glyphicon-chevron-left" title=
                                    "Previous Century"></span></th>
                                    <th class="picker-switch" colspan="6" data-action="pickerSwitch">1999-2099</th>
                                    <th class="next" data-action="next"><span class="glyphicon glyphicon-chevron-right" title="Next Century"></span></th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td colspan="8"><span class="decade" data-action="selectDecade" data-selection="2005">2000 - 2011</span><span class=
                                    "decade" data-action="selectDecade" data-selection="2017">2012 - 2023</span><span class="decade" data-action=
                                    "selectDecade" data-selection="2029">2024 - 2035</span><span class="decade" data-action="selectDecade" data-selection=
                                    "2041">2036 - 2047</span><span class="decade" data-action="selectDecade" data-selection="2053">2048 -
                                    2059</span><span class="decade" data-action="selectDecade" data-selection="2065">2060 - 2071</span><span class="decade"
                                    data-action="selectDecade" data-selection="2077">2072 - 2083</span><span class="decade" data-action="selectDecade"
                                    data-selection="2089">2084 - 2095</span><span class="decade" data-action="selectDecade" data-selection="2101">2096 -
                                    2107</span><span></span><span></span><span></span></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </li>
            <li class="picker-switch accordion-toggle">
                <table class="table-condensed">
                    <tbody>
                        <tr>
                            <td></td>
                        </tr>
                    </tbody>
                </table>
            </li>
        </ul>
    </div>
</div>

La línea azul oscura resalta el elemento td actual, que tiene la clase activa. En cada cambio (siempre hay un solo elemento td con esta clase), quiero actualizar otro elemento y establecerlo en el valor del atributo data-day del elemento actualmente activo (SIN tener que verificarlo periódicamente).

Ya tengo un pequeño fragmento que funciona, pero solo una vez y no del todo correcto. Si lo cambio una vez que obtiene el valor del td activo inicial y para cualquier cambio adicional, el valor sigue siendo el mismo (del elemento que lo muestra, un intervalo con id: fecha_inicio).

Se supone que este código logra el comportamiento deseado:

$('#start_datepicker').find("td").click(function()
{
    $("#start_date").text($('#start_datepicker').find(".active").attr("data-day"));
});

Creo que el error debe estar dentro de las 3 líneas anteriores.

Si necesito agregar más información para aclarar mi pregunta, por favor dígame.

Gracias de antemano John

0
JRsz 10 may. 2016 a las 16:20

3 respuestas

La mejor respuesta

La documentación pide escuchar dp.change. Esto debería funcionar:

$("#start_datepicker").on("dp.change", function (e) {
   $("#start_date").text(e.date);
});
1
Chintan 10 may. 2016 a las 14:27

Puede usar el evento "change" en la entrada que se agrega en lugar de buscar el elemento td:

$('#start_datepicker').find('input').change(function(){
    $("#start_date").text($(this).val());
});
0
Carlos Dominguez 10 may. 2016 a las 13:39

Tal vez el calendario dom se reemplaza sobre la marcha, por lo que su evento click se pierde. Prueba esto:

$('#start_datepicker').on('click', 'td', function()
{
    $("#start_date").text($('#start_datepicker').find(".active").attr("data-day"));
});

(ver http://api.jquery.com/on/)

1
Thomas Kekeisen 10 may. 2016 a las 13:23