简体   繁体   English

Bootstrap Datepicker不会在首次点击时触发

[英]Bootstrap datepicker doesn't fire on first click

I'm using the following code for displaying an inline datepicker; 我正在使用以下代码来显示内联日期选择器;

    $('.show_datepicker').datepicker({
        weekStart:  1,
        startDate : '2013-07-31',
        endDate : '2013-09-30',
        format : 'yyyy-mm-dd',
    }).on('changeDate', function(e){
        $('.datepicker-inline').on('click', '.datepicker_days:not(.disabled)', function() {
            $('.loading').show(); // Show loading icon
            $("#loading_div").load("loading_url.php",function(){
                $('.loading').hide(); // Hide loading icon when finished
            });
        });
    });

Problem 1 问题1

It all works very well, beside the fact that it doesn't fire when I click a day for the first time . 一切正常,除了我第一次单击一天时不会触发 After that initial click it's ok as long as I'm on the page. 第一次单击后,只要我在页面上就可以。 But everytime I refresh the page, I have to click any date for that first time to get it to work afterwards. 但是每次刷新页面时,我都必须第一次单击任何日期才能使它随后生效。

I suppose it's because of the seperate onclick event inside the function, but I need that to exclude all .disabled days from the call and just can't get it to work. 我想这是由于该函数内部存在单独的onclick事件,但是我需要这样做,以便从调用中排除所有.disabled天,并且无法使其正常工作。

Problem 2 问题2

console.log() tells me it's firing multiple times - when I click it for the second time it's firing 4 times, when I click it again it's firing 6 times, and so on. console.log()告诉我它多次触发 -当我第二次单击它时,它触发4次,当我再次单击它时,它触发6次,依此类推。

Any ideas will be greatly appreciated! 任何想法将不胜感激!

Update: 更新:

This is the html-code for the rendered datepicker: 这是呈现的日期选择器的html代码:

<div id="datepicker" class="show_datepicker table table-hover table-striped" data-date="2013-08-01">
<div class="datepicker datepicker-inline">
    <div class="datepicker-days" style="display: block;">
        <table class=" table-condensed">
        [...]
        <tbody>
            <tr>
                [...]
                <td class="datepicker_days day alternative" id="2013-08-12">12</td>
                <td class="datepicker_days day alternative" id="2013-08-13">13</td>
                <td class="datepicker_days day" id="2013-08-14">14</td>
                <td class="datepicker_days day inaktiv btn-link disabled" id="2013-08-15">15</td>
                <td class="datepicker_days day" id="2013-08-16">16</td>
                <td class="datepicker_days day" id="2013-08-17">17</td>
                <td class="datepicker_days day disabled inaktiv btn-link" id="2013-08-18">18</td>
                [...]
            </tr>
        </tbody>
        [...]
    </table>
    </div>
</div>

If changeDate does not fire when clicking on .disabled dates then you do not need to worry about filtering invalid dates. 如果单击.disabled日期时changeDate没有触发,则无需担心过滤无效日期。 You can remove the click handler which you correctly suspect is causing your multiple firings and not responding to the first click -- You didn't change the date yet so the click handler wasn't registered. 您可以删除您正确怀疑会引起多次触发并且不响应第一次点击的点击处理程序-您尚未更改日期,因此尚未注册该点击处理程序。

.on('changeDate', function(e){
    // won't fire on a .disabled date anyway
    $('.loading').show(); // Show loading icon
    $("#loading_div").load("loading_url.php",function(){
        $('.loading').hide(); // Hide loading icon when finished
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM