使用jQuery更改Chosen.js选择框的值 [英] Change the value of Chosen.js select box using jQuery
问题描述
我正在尝试更改具有 Chosen.js 覆盖的选择框的值.想法是当用户单击按钮时更改选定的值.
I'm trying to change the value of my select box which has Chosen.js overlay. The idea is to change the selected value when user clicks a button.
使用常规选择框,我可以通过以下方式更改值:
With regular select box I can change the value by doing:
$('#GroupsShowNext').unbind("click").click(function() {
var index = $("#GroupsViewGroups").prop("selectedIndex");
index += 1;
$('#GroupsViewGroups option').eq(index).attr('selected', 'selected');
$('#GroupsViewGroups').change();
});
但是使用Chosen.js,它不再起作用了..我尝试了很少的事情,但是没有任何效果.有任何想法如何使它起作用吗?
But with Chosen.js it doesn't work anymore..I have tried few things but nothing has worked. Any ideas how to get it work?
推荐答案
因此,发布此问题后,我继续尝试解决此问题,并偶然发现了解决方法.
So after posting this question I continued trying to solve this problem and happened to found out a way to do this.
$('#GroupsShowNext').unbind("click").click(function() {
var index = $("#GroupsViewGroups").prop("selectedIndex");
index += 1;
$('#GroupsViewGroups option').eq(index).attr('selected', 'selected');
$('#GroupsViewGroups').chosen().change();
$("#GroupsViewGroups").trigger("liszt:updated");
});
关键是将.chosen()放在.change()之前,然后触发"liszt:updated".它可以工作,但是我不知道这是否是最好的方法.
The key was to put .chosen() before .change() and then trigger "liszt:updated". It works but I don't know if this is the best way to do this..
如果您有更好的方法,请告诉我
If you have a better way to do this, please let me know
这篇关于使用jQuery更改Chosen.js选择框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!