如何为 Chosen (jQuery) 触发更改事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15102685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to trigger change event for Chosen (jQuery)
提问by Leo
Before I click reset button I choose "Company" in Chosen (dropdown list). The event occurs normally after I click reset. I choose "Company" again but event change
in dropdownlist doesn't occur.
在单击重置按钮之前,我在选择(下拉列表)中选择了“公司”。单击重置后,该事件正常发生。我再次选择“公司”,但change
下拉列表中的事件不会发生。
Could anyone tell me how to trigger the change event for dropdownlist after clicking reset button and then the same element?
谁能告诉我如何在单击重置按钮然后单击相同元素后触发下拉列表的更改事件?
The code I have so far:
我到目前为止的代码:
$("#mainMenu").change(function(e){
e.preventDefault();
loadFirstManu(true);
});
Code for the reset button:
重置按钮的代码:
$("#btn_reset").click(function() {
CKEDITOR.instances.ckeditor.setData('');
$('.mchosen').each(function() {
$(this).val('').trigger('liszt:updated');
$('#submenu').attr('disabled', 'disabled').html('');
$('#secondsubmenu').attr('disabled', 'disabled').html('');
$('#s-menu').removeClass('required').html('');
$('#secondsubmenu').removeClass('validate[required]');
$('#tabmenu').attr('disabled', 'disabled').html('');
$('#tab').removeClass('required').html('');
});
});
});
回答by bonyiii
This is what i figured out:
这是我想出来的:
$('#my-select').val(5).trigger("liszt:updated")
liszt:updated
is no longer working in new version of chosen instead use below as Alexandru Cojan'sanswer suggesting
liszt:updated
不再在选择的新版本中工作,而是使用下面作为Alexandru Cojan 的回答建议
trigger("chosen:updated");
回答by Alexandru Cojan
for newer version of chosen the event is "chosen:updated"
对于选择的较新版本,事件是“选择:更新”
$(selector).trigger("chosen:updated")
回答by Alexander
If i need just to refresh value in chosen select - .trigger('chosen:updated')
is enough. But if I have change handler and want it to be called - i need to do .trigger('chosen:updated').change()
如果我只需要刷新所选选择中的值 -.trigger('chosen:updated')
就足够了。但是如果我有更改处理程序并希望它被调用 - 我需要做.trigger('chosen:updated').change()
回答by Shehabic
I don't know if this is your case or not, but your code above should work,
我不知道这是否是你的情况,但你上面的代码应该可以工作,
$("#mainMenu").change(function(e){
e.preventDefault();
loadFirstManu(true);
});
but please notice that "change" event occurs on most browsers when you unfocusthe select input
但请注意,当您取消选择输入的焦点时,大多数浏览器都会发生“更改”事件
So when you click reset, just before executing the reset action the onchangeis triggered.
因此,当您单击reset 时,就在执行重置操作之前,将触发onchange。
Now try clicking outside the select input after changing the selection and see if it still works or not
现在尝试在更改选择后单击选择输入之外,看看它是否仍然有效
回答by apoq
Maybe you should try using .on, as your $('#mainMenu') may have changed somewhere (Can't say without an example). Try doing this:
也许您应该尝试使用 .on,因为您的 $('#mainMenu') 可能在某处发生了变化(没有例子就不能说)。尝试这样做:
$("body").on('change','#mainMenu',function(){
...
});
or any parent selector instead of "heavy" body
或任何父选择器而不是“重”体
回答by mohan.gade
If I am not wrong to understand you then you want to trigger the event change after click on the reset button. you can do this by simply adding one line to your code //code
如果我理解你没有错,那么你想在点击重置按钮后触发事件更改。你可以通过简单地在你的代码中添加一行来做到这一点 //code
$("#btn_reset").click(function(){
// your code here
$("#mainMenu").trigger('change');
//you can write this as per your requirements ie. at start or end.
});