简体   繁体   English

无法使用Select2以编程方式选择选项

[英]Can't programmatically select option using Select2

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. 我有一个Select2工作正常附加到选择列表,但当有人输入一个新项目,我重新绑定选择列表并尝试以编程方式选择新项目,它拒绝显示。
There are a number of other posts re this but their solutions don't work for me. 还有很多其他帖子,但他们的解决方案对我不起作用。

  1. Use .select2("val", value) . 使用.select2("val", value) Does nothing. 什么也没做。
  2. Use .select2("data", value) . 使用.select2("data", value) Does nothing. 什么也没做。 Not sure how this is supposed to be different. 不确定这应该是多么不同。
  3. Use the InitSelection option to set a value. 使用InitSelection选项设置值。 This leads to the following error message: Uncaught Error: Option 'initSelection' is not allowed for Select2 when attached to a select element . 这会导致以下错误消息: 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. 所以在这个最新的迭代中,我试图根据另一个建议的解决方案提供一个包含选择列表项中的id和text的数组,但仍然没有发生任何事情。 What am I doing wrong? 我究竟做错了什么? Is this impossible when using Select2 with a select list? 将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. 这必须通过绑定到模型中的DishID来引起问题,该模型在来自服务器时最初是空白的。 DropDownGroupListFor is just an HTML helper that allows for using OptionGroups in a Select but it must be the binding that is a problem. DropDownGroupListFor只是一个HTML助手,它允许在Select中使用OptionGroups,但它必须是一个有问题的绑定。 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,不确定最佳实践?

var $example = $(".js-example-programmatic").select2();
$example.val("CA").trigger("change"); 

You need to destroy your select2 before adding/modifying items. 在添加/修改项目之前,您需要销毁select2。 If you do, then your select2 will respond just like its bound first time 如果你这样做,那么你的select2将会像第一次绑定一样响应

Select2 Dropdown Dynamically Add, Remove and Refresh Items from Select2下拉列表动态添加,删除和刷新项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM