简体   繁体   English

Bootstrap 多选模糊事件未触发

[英]Bootstrap multiselect blur event not triggering

I'm trying to do some logic after user completes a selection and moves away from a bootstrap multi-select dropdown.在用户完成选择并离开引导程序多选下拉菜单后,我正在尝试做一些逻辑。

I tried hooking into the blur event:我尝试连接到模糊事件:

      <select id="select1" multiple="multiple" class="form-control" data-bind="options: availableValues, selectedOptions: selectedValues, multiselect: {
    includeSelectAllOption: true
  }, event: { blur: $root.onBlur}">
      </select>  

Defining a onBlur function in my viewModel:在我的 viewModel 中定义一个 onBlur 函数:

onBlur: function(){
    alert('OnBlur');
}

But it never kicks-in.但它永远不会启动。 I even tried setting the event binding directly without the $root.onBlur , just blur: onBlur but didn't work.我什至尝试在没有$root.onBlur情况下直接设置事件绑定,只是blur: onBlur但没有用。

Here is a sample JS fiddle:这是一个示例 JS 小提琴:

https://jsfiddle.net/gtzatrL2/ https://jsfiddle.net/gtzatrL2/

Am I missing something?我错过了什么吗? or is the blur event not applicable to bootstrap multi-selects?还是模糊事件不适用于引导多选?

The reason I want to hook into blur is because I have some backend logic to do through Ajax after ALL items have been selected.我想要模糊的原因是因为在选择了所有项目后,我有一些后端逻辑要通过 Ajax 来完成。 If I subscribe to my observableArray changes it will trigger my backend call for each item added which is not what I want.如果我订阅我的 observableArray 更改,它将触发我对添加的每个项目的后端调用,这不是我想要的。

Bootstrap is actually hiding the original select element and showing a div in its place, so the blur on the select never gets hit because it isn't being focused in the first place. Bootstrap 实际上隐藏了原始的 select 元素并在其位置显示了一个 div,因此 select 上的模糊永远不会被击中,因为它首先没有被聚焦。

I think because the bootstrap controls are using divs it might be impossible to support blur at all because divs aren't normally focusable.我认为因为引导控件使用 div,所以可能根本不可能支持模糊,因为 div 通常不可聚焦。 There are a couple of workarounds but it might not work in this particular case.有几种解决方法,但在这种特殊情况下可能不起作用。 See How to blur the div element?请参阅如何模糊 div 元素?

One option instead - you could rate-limit your observableArray so that the subscription doesn't fire immediately every time an option is selected.一种选择 - 您可以对 observableArray 进行速率限制,以便每次选择一个选项时都不会立即触发订阅。 This will also reduce the backend calls, but it does not guarantee it won't fire before the user is finished.这也将减少后端调用,但并不能保证它不会在用户完成之前触发。

http://knockoutjs.com/documentation/rateLimit-observable.html http://knockoutjs.com/documentation/rateLimit-observable.html

this.selectedValues = ko.observableArray().extend({ rateLimit: { timeout: 500, method: "notifyWhenChangesStop" } });

this.selectedValues.subscribe(function() {
    alert("BLUR: " + this.selectedValues().length + " elements selected");
});

Here's a working example -- when the user 'tabs' away to the second input the blur event callback displays how many elements were selected这是一个工作示例——当用户“tabs”到第二个输入时,模糊事件回调显示选择了多少元素

 var ViewModel = function() { this.availableValues = ko.observableArray(["Option A", "Option B", "Option C"]); this.selectedValues = ko.observableArray(); this.onBlur = function() { alert("BLUR: " + this.selectedValues().length + " elements selected"); } }; ko.applyBindings(new ViewModel());
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> <select id="select1" multiple="multiple" class="form-control" data-bind="options: availableValues, selectedOptions: selectedValues, multiselect: { includeSelectAllOption: true },event: { blur: $root.onBlur}"> <input type="text">

fyi here's a response from developers github page仅供参考,这是来自开发人员 github 页面的回复

https://github.com/davidstutz/bootstrap-multiselect/issues/1020 https://github.com/davidstutz/bootstrap-multiselect/issues/1020

basically say's that you should call onDropdownHide function wich trigger a kind of "blur" for multiselect plugin基本上是说你应该调用onDropdownHide函数来触发多选插件的一种“模糊”

Bootstrap-select exposes a few events for hooking into select functionality developer.snapappointments.com Bootstrap-select 公开了一些用于挂钩到选择功能的事件developer.snapappointments.com

So one could refer to hide.bs.select or hidden.bs.select.所以可以参考 hide.bs.select 或 hidden.bs.select。 You could then access the selected values directly from the event parameter or if you have an Knockout observableArray attached to ie selectedOptions;然后,您可以直接从 event 参数访问选定的值,或者如果您有一个附加到 ie selectedOptions 的 Knockout observableArray; like so:像这样:

$(document).ready(function () {
     $('#select1').on('hide.bs.select', (e) => {
        console.log({ obs: self.selectedValues(), event: $(e.currentTarget).selectpicker('val') });
    });
});

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

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