使用 jQuery 更改 Chosen.js 选择框的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9663425/
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
Change the value of Chosen.js select box using jQuery
提问by Spitz
I'm trying to change the value of my select box which has Chosen.jsoverlay. The idea is to change the selected value when user clicks a button.
我正在尝试更改具有Chosen.js叠加层的选择框的值。这个想法是在用户单击按钮时更改选定的值。
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();
});
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?
但是使用 Chosen.js 它不再起作用了..我尝试了一些东西,但没有任何效果。任何想法如何让它工作?
回答by Spitz
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");
});
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..
关键是将 .chosen() 放在 .change() 之前,然后触发“liszt:updated”。它有效,但我不知道这是否是最好的方法。
If you have a better way to do this, please let me know
如果您有更好的方法来做到这一点,请告诉我
回答by Semyaz
I stumbled across this trying to figure it out myself.
我偶然发现了这个,试图自己弄清楚。
I was able to do it with a combination of changing the selected index on the original field, and then triggering the liszt:updated
event on that field:
我能够通过更改原始字段上的选定索引,然后liszt:updated
在该字段上触发事件的组合来做到这一点:
$('#GroupsViewGroups')[0].selectedIndex = $('#GroupsViewGroups')[0].selectedIndex + 1;
$('#GroupsViewGroups').trigger('liszt:updated');
回答by Luiz Carlos Lucena
Just an update (I found this page from google, maybe other people will find it also)
只是一个更新(我从谷歌找到了这个页面,也许其他人也会找到它)
I've tryed to comment the @splitz's answer but I couldn't because I still don't have enough reputation.
我试图评论@splitz 的答案,但我不能,因为我仍然没有足够的声誉。
The new way to do it is:
新的方法是:
$('#GroupsShowNext').unbind("click").click(function() {
var index = $("#GroupsViewGroups").prop("selectedIndex");
index += 1;
$('#GroupsViewGroups option').eq(index).attr('selected', 'selected');
$("#GroupsViewGroups").trigger("chosen:updated");
});
Now, the trick is the "chosen:updated".
现在,诀窍是“选择:更新”。
Updated answer found on: Changing selection in a select with the Chosen plugin
在以下位置找到更新的答案:使用选择的插件更改选择中的选择
回答by Marta
With the plugin Chosen.js it works.
使用插件 Chosen.js 它可以工作。
var myIndex = 4;
$("select#GroupsViewGroups").prop('selectedIndex', myIndex);