如何使用 JqGrid 更改 select2 下拉列表的选定值? [英] How do I change selected value of select2 dropdown with JqGrid?

查看:15
本文介绍了如何使用 JqGrid 更改 select2 下拉列表的选定值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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:触发 JUST 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 与名称相同).首先我在我的选择上初始化 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屋!

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