jQuery Chosen plugin add options dynamically

后端 未结 5 1637
闹比i
闹比i 2020-12-03 00:38

I make a jQuery Chosen drop-down like this:

$(\'.blah\').chosen();

I can\'t find how I can add options, something like:

5条回答
  •  感情败类
    2020-12-03 00:59

    You can call this function to add element to chosen after you save the element to server using Ajax:

    function appendToChosen(id,value){
        $('.blah')
            .append($('')
            .val(id)
            .attr('selected', 'selected')
            .html(value)).trigger('liszt:updated');
    }
    

    Ajax call:

    $.ajax({
        type: 'POST',
        url: 'savepage.php',
        data: $('#modal-form form').serialize(),
    
        success: function(data, status) {
            appendToChosen(data[0],data[1]);
        },
        error: function (response) {
            alert(response);
        }
        }).always(function(data, status) {
            //hide loading
        });
    

提交回复
热议问题