javascript 无法使用 Select2 以编程方式选择选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25128770/
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
Can't programmatically select option using Select2
提问by user3775501
I have a Select2 working fine attached to a select list but when someone enters a new item and I rebind the select list and try to programmatically select the new item, it refuses to display.
There are a number of other posts re this but their solutions don't work for me.
我有一个 Select2 工作正常附加到选择列表但是当有人输入一个新项目并且我重新绑定选择列表并尝试以编程方式选择新项目时,它拒绝显示。
还有很多其他的帖子,但他们的解决方案对我不起作用。
- Use
.select2("val", value)
. Does nothing. - Use
.select2("data", value)
. Does nothing. Not sure how this is supposed to be different. - Use the
InitSelection
option to set a value. This leads to the following error message:Uncaught Error: Option 'initSelection' is not allowed for Select2 when attached to a select element
.
- 使用
.select2("val", value)
. 什么也没做。 - 使用
.select2("data", value)
. 什么也没做。不知道这应该是如何不同的。 - 使用该
InitSelection
选项设置一个值。这会导致以下错误消息:Uncaught Error: Option 'initSelection' is not allowed for Select2 when attached to a select element
.
In the Ajax success function after adding the new option to the database, I have the following Javascript:
在将新选项添加到数据库后的 Ajax 成功函数中,我有以下 Javascript:
BindDishes(RestaurantID); //rebinds the select list successfully
$('#hidDishID').val = data.DishID; //populates a hidden field
var arr = '[{id: "' + data.DishID + '", text: "' + data.DishName + '"}]';
$("#dd").select2("data", arr);
So in this latest iteration I have tried to supply an array with the id and text from the select list item as per another suggested solution but still nothing occurs. What am I doing wrong? Is this impossible when using Select2 with a select list?
所以在这个最新的迭代中,我尝试根据另一个建议的解决方案从选择列表项中提供一个带有 id 和文本的数组,但仍然没有任何反应。我究竟做错了什么?将 Select2 与选择列表一起使用时,这是不可能的吗?
Edit: I have the following line in MVC that creates the Select list:
编辑:我在 MVC 中有以下行来创建选择列表:
@Html.DropDownGroupListFor(m => m.DishID, Model.GroupedSelectList, "Select a dish", new { id="dd",style="width:470px;" })
which must be causing the problem by binding to DishID from the model which was originally blank when it came from the server. DropDownGroupListFor is just an HTML helper that allows for using OptionGroups in a Select but it must be the binding that is a problem. What is the best approach here, to bind in a different way or to somehow update the model's DishID, not sure of the best practice?
这一定是通过从来自服务器的模型中绑定到 DishID 来导致问题的,该模型最初是空白的。DropDownGroupListFor 只是一个 HTML 帮助器,它允许在 Select 中使用 OptionGroups,但它必须是有问题的绑定。这里最好的方法是什么,以不同的方式绑定或以某种方式更新模型的 DishID,不确定最佳实践?
回答by pmeyer
var $example = $(".js-example-programmatic").select2();
$example.val("CA").trigger("change");
回答by Sami
You need to destroy your select2 before adding/modifying items. If you do, then your select2 will respond just like its bound first time
您需要在添加/修改项目之前销毁您的 select2。如果你这样做,那么你的 select2 将像第一次绑定一样响应
Select2 Dropdown Dynamically Add, Remove and Refresh Items from