admin 管理员组文章数量: 1086019
I have a multiselect dropdown element with check boxes and a change function for that element in my javascript file. But the change function is triggered every time a check box is clicked. I would like the change function to be triggered only after the user exits the select dropdown or hits 'enter' so it only runs once if a user checks five boxes instead of running five times (immediately after each new box is checked). How can I do this? Here is my current code:
html element:
<select id="vo_select_mobile_brands" multiple="multiple">
<option>Select a country</option>
</select>
javascript dynamic addition of checkbox options:
$el = $('#vo_select_mobile_brands');
$el.empty()
$.each(_vo_mobile_brand_ids_list, function(idx, mobile_brand_id) {
$el.append($("<option></option>")
.attr("value", mobile_brand_id).text(mobile_brand_id));
});
$el.multiselect('destroy')
$el.multiselect({
includeSelectAllOption: true
});
$el.multiselect()
javascript change function:
$('#vo_select_mobile_brands').change(function() {
_vo_selected_mobile_brands = [];
selected_objects = $("#vo_select_mobile_brands option:selected")
for(var i = 0; i < selected_objects.length; i++){
//do something with the selected items
}
});
I have a multiselect dropdown element with check boxes and a change function for that element in my javascript file. But the change function is triggered every time a check box is clicked. I would like the change function to be triggered only after the user exits the select dropdown or hits 'enter' so it only runs once if a user checks five boxes instead of running five times (immediately after each new box is checked). How can I do this? Here is my current code:
html element:
<select id="vo_select_mobile_brands" multiple="multiple">
<option>Select a country</option>
</select>
javascript dynamic addition of checkbox options:
$el = $('#vo_select_mobile_brands');
$el.empty()
$.each(_vo_mobile_brand_ids_list, function(idx, mobile_brand_id) {
$el.append($("<option></option>")
.attr("value", mobile_brand_id).text(mobile_brand_id));
});
$el.multiselect('destroy')
$el.multiselect({
includeSelectAllOption: true
});
$el.multiselect()
javascript change function:
$('#vo_select_mobile_brands').change(function() {
_vo_selected_mobile_brands = [];
selected_objects = $("#vo_select_mobile_brands option:selected")
for(var i = 0; i < selected_objects.length; i++){
//do something with the selected items
}
});
Share
Improve this question
edited Feb 25, 2017 at 17:09
Mark Schultheiss
34.2k12 gold badges72 silver badges113 bronze badges
asked Feb 25, 2017 at 16:40
David LewineDavid Lewine
1411 gold badge2 silver badges14 bronze badges
1
- Possible duplicate of Javascript - Which event to use for multiselect change – Vivek Athalye Commented Feb 25, 2017 at 16:51
3 Answers
Reset to default 4You can execute your js function on a button click with button click event
like
$('#ButtonID').click(function() {
_vo_selected_mobile_brands = [];
selected_objects = $("#vo_select_mobile_brands option:selected")
for(var i = 0; i < selected_objects.length; i++){
//do something with the selected items
}
});
So it will get fired only when user want to submit after selecting multiple values from dropdown.
OR
If you still need to execute it on change event of dropdown when 5 items are selected then you can get count of the selected values and fire the function if selected items are equal to 5
$('#vo_select_mobile_brands').change(function() {
var count = $("#vo_select_mobile_brands :selected").length;
if(count==5)
alert(count);
});
See fiddle : https://jsfiddle/4zabsa9e/7/
Maybe you are looking for a delay, so that if several changes are made within a short time, your code only executes after the last one:
var timer = null;
$('#vo_select_mobile_brands').change(function() {
clearTimeout(timer); // cancel and restart timer
timer = setTimeout(function () {
_vo_selected_mobile_brands = [];
selected_objects = $("#vo_select_mobile_brands option:selected")
for(var i = 0; i < selected_objects.length; i++){
//do something with the selected items
}
}, 1000); // after one second of idle time
});
So only trigger when the select looses focus/blur perhaps?:
$("#vo_select_mobile_brands").on('focusout blur',function(){
//will return an array of the values for the selected options
var myselectedvalues = $(this).val();
});
The focusout event is sent to an element when it, or any element inside of it, loses focus. This is distinct from the blur event in that it supports detecting the loss of focus from parent elements (in other words, it supports event bubbling).
IF you manually change values via script, you can trigger the event:
$("#vo_select_mobile_brands").trigger("focusout");
版权声明:本文标题:javascript - How to trigger a change event only after all multiselect selections have been made - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744075454a2529267.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论