jQuery选择 - 如何动态更改选项,同时保留选定的选项

时间:2014-01-27 16:13:13

标签: jquery json jquery-chosen

我目前有一个包含两个下拉列表的页面,一个是chosen选择输入,另一个是包含组织的普通选择。

当所选组织发生变化时,我正在尝试动态更新chosen选项中可用的选项,但请记住以前选择的选项。

JS

// when the organisation is changed, re-populate list
$('#cc_list_organisation').change(function() {

    $.ajax({
        type: 'POST',
        url: '/organisation/ajax_find_employees',
        dataType: 'json',
        data: { organisation_id: $('#cc_list_organisation').val() },
        success: function(data) {
            $('#cc_list').empty().append(data.html);


            $('ul.chzn-results').empty();

            var choices = $('.chzn-choices').html(); // save previous choices in variable

            $("#cc_list").trigger("liszt:updated");

            $('.chzn-choices').empty().html(choices); // 

            /*

            // was trying to do it manually but kept getting an error, and then I found the above
            var results = $.parseJSON(data.js);

            response($.map(results, function(item) {

                $('ul.chzn-results').append('<li id="cc_list_chzn_o_' + item.index + '" class="active-result">' + item.name + '</li>');
                }));
            */
        }

    });

    return false;

});

JSON

{
   "html":"\u003Coption value=\"214\"\u003EPerson 1\u003C/option\u003E\u003Coption value=\"367\"\u003EPerson 2\u003C/option\u003E",
   "js":"{ \"index\": 0, \"name\": \"Person1\" }, { \"index\": 1, \"name\": \"Person 2\" }"
}

0 个答案:

没有答案