在我正在开发的代码库中,存在这种类型的回调绑定(bind),只要任何输入发生更改,就必须发生某些事情
$(document.body).on('change', '.input-sm', function (){
...
})
问题是,一些输入短信是通过时钟选择器更改的,这不会触发“更改”事件。我该如何做这个工作?理想情况下,我希望时钟选择器触发更改事件。
http://jsfiddle.net/4zg3w5sj/7/
编辑:回调一次被绑定(bind)到带有时钟选择器的多个输入,因此我无法使用输入变量来触发更改事件(除非我明确地迭代我猜测的输入)
最佳答案
您可以使用时钟选择器回调
beforeHourSelect : callback function triggered before user makes an hour selection
afterHourSelect : callback function triggered after user makes an hour selection
beforeDone : callback function triggered before time is written to input
afterDone : callback function triggered after time is written to input
input.clockpicker({
autoclose: true,
afterDone: function() {
input.trigger("change");
}
});
我已经解决了这个问题
该插件触发了更改事件,但他们使用 triggerHandler而不是trigger这意味着你不能在 body 上添加监听器,你必须直接监听输入
// Hours and minutes are selected
ClockPicker.prototype.done = function() {
raiseCallback(this.options.beforeDone);
this.hide();
var last = this.input.prop('value'),
value = leadingZero(this.hours) + ':' + leadingZero(this.minutes);
if (this.options.twelvehour) {
value = value + this.amOrPm;
}
this.input.prop('value', value);
if (value !== last) {
this.input.triggerHandler('change');
if (! this.isInput) {
this.element.trigger('change');
}
}
if (this.options.autoclose) {
this.input.trigger('blur');
}
raiseCallback(this.options.afterDone);
};
请参阅此处的修复
var input = $('#input-a');
var value = input.val();
// bind multiple inputs
$('.myinput').clockpicker({
autoclose: true,
afterDone: function() {
console.log("test");
}
});
// in the actual code it's not tied to an id but to a non-unique class
// does not trigger if changed by clock-picker
$(".myinput").on('change', function(){
console.log("!!!!")
})
关于javascript - JQuery Clockpicker 不触发输入上的更改事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35775639/