简体   繁体   English

JQuery Timepicker - 更改事件处理程序不工作?

[英]JQuery Timepicker - On change event handler not working?

I am using jQuery timepicker ( http://timepicker.co/ ) with the following input fields.我正在使用带有以下输入字段的 jQuery timepicker ( http://timepicker.co/ )。 When I select an option from the dropdown, the jquery on change event handler doesnt seem to fire.当我 select 从下拉列表中选择一个选项时,更改事件处理程序上的 jquery 似乎没有触发。 It only seems to work when I manually enter a value and hit the tab key?它似乎只有在我手动输入一个值并按下 tab 键时才有效?

<input id="TmOnSite" class="timepicker js-tm-on-site" type="text" value="" name="TmOnSite" aria-invalid="false">
<input id="TmOffSite" class="timepicker js-tm-on-site" type="text" value="" name="TmOffSite" aria-invalid="false">
<input id="TmTotalHrsOnSite" class="" type="text" value="" readonly="true" name="TmTotalHrsOnSite">

<script>

$(function () {

     TimePicker();

});

var TimePicker = function () {

    if ($(".timepicker").length === 0) { return; }

   $(".timepicker").timepicker({
       timeFormat: 'HH:mm',
       interval: 30,
       scrollbar: true
   });

};

var tmTotalHrsOnSite = function () {

    $(document).on('change select', '.js-tm-on-site', function (e) {

       if ($("#TmOnSite") && $("#TmOffSite")) {

           var startTime = moment($("#TmOnSite").val(), "HH:mm");
           var endTime = moment($("#TmOffSite").val(), "HH:mm");
           var duration = moment.duration(endTime.diff(startTime));
           $("#TmTotalHrsOnSite").val(duration.asHours().toFixed(2));
        }
    });

}();

</script>

Use change option:使用change选项:

$(".timepicker").timepicker({
   timeFormat: 'HH:mm',
   interval: 30,
   scrollbar: true,
   change: tmTotalHrsOnSite
});

 $(function () { TimePicker(); }); var TimePicker = function () { if ($(".timepicker").length === 0) { return; } $(".timepicker").timepicker({ timeFormat: 'HH:mm', interval: 30, scrollbar: true, change: tmTotalHrsOnSite }); }; function tmTotalHrsOnSite () { console.log('changed.'); if ($("#TmOnSite") && $("#TmOffSite")) { var startTime = moment($("#TmOnSite").val(), "HH:mm"); var endTime = moment($("#TmOffSite").val(), "HH:mm"); var duration = moment.duration(endTime.diff(startTime)); $("#TmTotalHrsOnSite").val(duration.asHours().toFixed(2)); } };
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/timepicker/1.3.5/jquery.timepicker.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/timepicker/1.3.5/jquery.timepicker.js"></script> <input id="TmOnSite" class="timepicker js-tm-on-site" type="text" value="" name="TmOnSite" aria-invalid="false"> <input id="TmOffSite" class="timepicker js-tm-on-site" type="text" value="" name="TmOffSite" aria-invalid="false"> <input id="TmTotalHrsOnSite" class="" type="text" value="" readonly="true" name="TmTotalHrsOnSite">

Events don't fire if JS changes values pragmatically.如果 JS 以务实的方式更改值,则不会触发事件。 Though on the other hand JS can pragmatically trigger them.虽然另一方面 JS 可以务实地触发它们。 When you choose time in the list the plugin does not trigger it.当您在列表中选择时间时,插件不会触发它。 Most of the time plugins like this one will expose their own API with events to bind to.大多数情况下,像这样的插件会公开自己的 API,并带有要绑定到的事件。 At least if they are good plugins.至少如果它们是好的插件。 Update event is a must.更新事件是必须的。 Check out http://timepicker.co/options/ for api There is a description of Change event查看http://timepicker.co/options/的 api 有关于 Change 事件的描述

$('.js-tm-on-site').timepicker({
    change: function(time) {
        /* Code here */
    }
});

This will run the code on each change.这将在每次更改时运行代码。 But you have to bind this event at the time when the plugin is initialized.但是你必须在插件初始化的时候绑定这个事件。 This may require restructuring the code.这可能需要重构代码。

Here is the best answer I think please follow this Can you try这是我认为最好的答案,请按照此您可以尝试

$(document).on('change', '.yourSelector', function (e) {
  //code is here work for me
}

This should work这应该工作

$('#TmOnSite').on('changeTime', function() {
    var x = $("#TmOnSite").val();                    
});

This is the solution:这是解决方案:

$('document').ready(
 $('.timePicker').datetimepicker({
  //All de configuration of your datepicker
 });

 // Note that if you want the event to be for a specific element, 
 //  you must put the element ID instead of <<timePicker>>
 $('.timePicker').on('dp.change', function() {
  // What do you want to execute when the value change
 });
)

I am not able to get the change event to fire at all when clicking the select dropdown menus.单击选择下拉菜单时,我根本无法触发change事件。 It does fire when I type text into the input field, but not when I select a dropdown time using only the mouse.当我在输入字段中输入文本时它确实会触发,但当我仅使用鼠标选择下拉时间时不会触发 To get that to work, you can just use onBlur for the blur event, which fires when you select a time from the dropdown.onBlur发挥作用,您可以将onBlur用于blur事件,当您从下拉列表中选择时间时会触发该事件。

$(document).on('blur', '.yourSelector', function (e) {
     console.log("New value :::" + e.target.value + "|");
}

for me this solution working fine.对我来说,这个解决方案工作正常。

 <input
    type="text"
    v-model="time"
    class="form-control timepicker"
    name="time"
 />

 data() {
    return {
       time: "",
    };
  },

mounted() {
    var vm = this;
    $(".timepicker").timepicker({
      timeFormat: "h:mm p",
      interval: 60,
      scrollbar: true,
      change: function (time) {
         vm.time = $(this).val();
      },
    });
  },
$("#EventStartTime").on('change',
   function (time) {
    console.log("Selected time : " + time.currentTarget.value);
});

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

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