使用选择二不能以编程方式选择选项 [英] Can't programmatically select option using Select2

查看:199
本文介绍了使用选择二不能以编程方式选择选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个选择二做工精细附加到选择列表,但是当有人进入一个新的项目,我重新绑定选择列表,并尝试以编程方式选择新的项目,它拒绝显示。结果
还有一些其他职位重新这一点,但他们的解决方案不为我工作。


  1. 使用 .select2(VAL,值)。什么也没做。

  2. 使用 .select2(数据,价值)。什么也没做。不知道这是怎么应该是不同的。

  3. 使用 InitSelection 选项设置一个值。这导致了以下错误消息:未捕获的错误:选项initSelection连接到选择的元素时不允许选择二

在添加新的选项到数据库后,阿贾克斯成功的功能,我有以下的Javascript:

  BindDishes(RestaurantID); //成功地重新绑定选择列表
$('#hidDishID')VAL = data.DishID。 //填充隐藏字段
VAR ARR ='[{ID:'+ data.DishID +',文:'+ data.DishName +'}]';
$(#DD)选择2(数据,ARR)。

因此​​,在这个最新的迭代我试图从选择列表项提供的ID和文字数组按另一个建议的解决方案,但仍然没有发生。我究竟做错了什么?使用选择二使用选择列表时,这是不可能的?

修改:我在MVC以下行创建的选择列表:

  @ Html.DropDownGroupListFor(M = GT; m.DishID,Model.GroupedSelectList,选择一个菜,新{ID =DD,风格=WIDTH:470px; })

这必须由这原本是空白的,当它从服务器附带的模型结合DishID会引起问题。 DropDownGroupListFor仅仅是一个HTML辅助,其允许在一选择使用OptionGroups但它必须是结合这是一个问题。什么是最好的方法在这里,以不同的方式结合或以某种方式更新模型的DishID,不能确定最好的做法的


解决方案

 变量$例如= $(JS-例如程序化。)选择2()。
$ example.val(CA)的触发器(变)。

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.

  1. Use .select2("val", value). Does nothing.
  2. Use .select2("data", value). Does nothing. Not sure how this is supposed to be different.
  3. 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.

In the Ajax success function after adding the new option to the database, I have the following 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?

Edit: I have the following line in MVC that creates the Select list:

@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?

解决方案

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

这篇关于使用选择二不能以编程方式选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆