如何使用JqGrid更改select2下拉列表的选定值? [英] How do I change selected value of select2 dropdown with JqGrid?
问题描述
我正在使用Oleg的 select2演示,但我想知道是否有可能更改下拉菜单中当前选择的值.
I'm using Oleg's select2 demo, but I am wondering whether it would be possible to change the currently selected value in the dropdown menu.
例如,如果加载的四个值是:"Any", "Fruit", "Vegetable", "Meat"
,并且下拉列表默认为"Any"
,那么我如何在JqGrid事件loadComplete
中将其更改为"Fruit"
?
For example, if the four values loaded were: "Any", "Fruit", "Vegetable", "Meat"
and the dropdown list defaulted to "Any"
, how would I be able to change that to "Fruit"
in the JqGrid event loadComplete
?
这可能吗?
推荐答案
对于select2版本> = 4.0.0
其他解决方案可能不起作用,但是以下示例应该起作用.
The other solutions might not work, however the following examples should work.
$('select').val('1').trigger('change');
解决方案2:导致仅select2更改事件触发
$('select').val('1').trigger('change.select2');
有关这些示例,请参见 jsfiddle .感谢@minlare提供解决方案2.
See this jsfiddle for examples of these. Thanks to @minlare for Solution 2.
说我有一个最好的朋友,请选择人们的名字.因此,Bob,Bill和John(在此示例中,我假定Value与名称相同).首先,我在select上初始化select2:
Say I have a best friend select with people's names. So Bob, Bill and John (in this example I assume the Value is the same as the name). First I initialize select2 on my select:
$('#my-best-friend').select2();
现在,我在浏览器中手动选择Bob.接下来鲍勃做了一些调皮的事,我不再喜欢他了.因此系统为我取消选择了Bob:
Now I manually select Bob in the browser. Next Bob does something naughty and I don't like him anymore. So the system unselects Bob for me:
$('#my-best-friend').val('').trigger('change');
或者说我让系统选择列表中的下一个而不是Bob:
Or say I make the system select the next in the list instead of Bob:
// I have assume you can write code to select the next guy in the list
$('#my-best-friend').val('Bill').trigger('change');
有关Select 2网站的说明(请参阅已弃用和删除的方法)对于其他人:
Notes on Select 2 website (see Deprecated and removed methods) that might be useful for others:
.select2('val') "val"方法已被弃用,并将在Select2 4.1中删除.不推荐使用的方法不再包含triggerChange参数.
.select2('val') The "val" method has been deprecated and will be removed in Select2 4.1. The deprecated method no longer includes the triggerChange parameter.
您应该直接在基础元素上调用.val.如果需要第二个参数(triggerChange),则还应该在元素上调用.trigger("change").
You should directly call .val on the underlying element instead. If you needed the second parameter (triggerChange), you should also call .trigger("change") on the element.
$('select').val('1').trigger('change'); // instead of $('select').select2('val', '1');
这篇关于如何使用JqGrid更改select2下拉列表的选定值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!