select2 中的预选值

新手上路,请多包涵

Texbox 使用 Select2 动态填充远程调用以及如何设置预选值。这是代码

<input type="hidden" id="e6">

$("#e6").select2({
    placeholder: "Search for a movie",
    minimumInputLength: 1,
    ajax: {
        url: url,
        dataType: 'jsonp',
        data: function (term, page) {
            return {
                q: term, // search term
                page_limit: 10, };
        },
        results: function (data, page) {
                return {results: data};
        }
    }
});

我试过这个来预选值 1049

$('#e6').select2('val', '1049');

但这并没有在文本框上设置值。

有任何想法吗?

原文由 Smith 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 371
2 个回答

如果有人想知道如何解决这个问题

$("#e6").select2('data', {id: '1049', text: 'MyLabel'});

原文由 Smith 发布,翻译遵循 CC BY-SA 3.0 许可协议

对我有用的解决方案(在文档中有描述)是:

 $('#mySelect2').val('1'); // Select the option with a value of '1'
$('#mySelect2').trigger('change'); // Notify any JS components that the value changed

原文由 Alice 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏