使用 Chosen 插件更改选择中的选择 [英] Changing selection in a select with the Chosen plugin

查看:33
本文介绍了使用 Chosen 插件更改选择中的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Chosen 插件更改选择中当前选定的选项.

I'm trying to change the currently selected option in a select with the Chosen plugin.

文档 包括更新列表和在选择选项时触发事件,但没有(我可以看到)在外部更改当前选定的值.

The documentation covers updating the list, and triggering an event when an option is selected, but nothing (that I can see) on externally changing the currently selected value.

我制作了一个 jsFiddle 来演示代码以及我尝试更改选择的方法:

I have made a jsFiddle to demonstrate the code and my attempted ways of changing the selection:

$('button').click(function() {
    $('select').val(2);
    $('select').chosen().val(2);
    $('select').chosen().select(2);
});

推荐答案

来自 docs:您需要在字段上触发'chosen:updated'事件

From the "Updating Chosen Dynamically" section in the docs: You need to trigger the 'chosen:updated' event on the field

$(document).ready(function() {

    $('select').chosen();

    $('button').click(function() {
        $('select').val(2);
        $('select').trigger("chosen:updated");
    });

});

注意:1.0 之前的版本使用以下内容:

NOTE: versions prior to 1.0 used the following:

$('select').trigger("liszt:updated");

这篇关于使用 Chosen 插件更改选择中的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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