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

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

问题描述

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

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);
});


推荐答案

从<动态更新动态选择部分a href =http://harvesthq.github.com/chosen/> docs :您需要在该字段上触发'selected: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天全站免登陆