Jquery Chosen plugin - dynamically populate list by Ajax

后端 未结 12 1025
野性不改
野性不改 2020-12-04 11:14

Im trying to build my dropdown menu using the plugin Chosen for Multiple Select . Here\'s to behavior I\'m based on:

http://jsfiddle.net/JfLvA/

相关标签:
12条回答
  • 2020-12-04 12:02

    Ashirvad's answer no longer works. Note the class name changes and using the option element instead of the li element. I've updated my answer to not use the deprecated "success" event, instead opting for .done():

    $('.chosen-search input').autocomplete({
        minLength: 3,
        source: function( request, response ) {
            $.ajax({
                url: "/some/autocomplete/url/"+request.term,
                dataType: "json",
                beforeSend: function(){ $('ul.chosen-results').empty(); $("#CHOSEN_INPUT_FIELDID").empty(); }
            }).done(function( data ) {
                    response( $.map( data, function( item ) {
                        $('#CHOSEN_INPUT_FIELDID').append('<option value="blah">' + item.name + '</option>');
                    }));
    
                   $("#CHOSEN_INPUT_FIELDID").trigger("chosen:updated");
            });
        }
    });
    
    0 讨论(0)
  • 2020-12-04 12:06

    This might be helpful. You have to just trigger an event.

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

    Where "DropDownID" is ID of <select>.

    More info here: http://harvesthq.github.com/chosen/

    0 讨论(0)
  • 2020-12-04 12:07

    try this:

    $('.chzn-choices input').autocomplete({
      source: function( request, response ) {
        $.ajax({
          url: "/change/name/autocomplete/"+request.term+"/",
          dataType: "json",
          beforeSend: function(){$('ul.chzn-results').empty();},
          success: function( data ) {
            response( $.map( data, function( item ) {
              $('ul.chzn-results').append('<li class="active-result">' + item.name + '</li>');
            }));
          }
        });
      }
    });
    
    0 讨论(0)
  • 2020-12-04 12:09

    If you are generating select tag from ajax, add this inside success function:

    $('.chosen').chosen();
    

    Or, If you are generating select tag on clicking add more button then add:

    $('.chosen').chosen();
    

    inside the function.

    0 讨论(0)
  • 2020-12-04 12:10

    Like Vicky suggested, Select2 comes with the AJAX features inbuilt and looks like a great plugin.

    If you dont want to switch from Chosen, try AJAX-Chosen https://github.com/meltingice/ajax-chosen

    0 讨论(0)
  • 2020-12-04 12:10
    var object = $("#lstValue_chosen").find('.chosen-choices').find('input[type="text"]')[0];
    var _KeyCode = event.which || event.keyCode;
    if (_KeyCode != 37 && _KeyCode != 38 && _KeyCode != 39 && _KeyCode != 40) { 
    
        if (object.value != "") {
            var SelectedObjvalue = object.value;
            if (SelectedObjvalue.length > 0) {
                var obj = { value: SelectedObjvalue };
                var SelectedListValue = $('#lstValue').val();
                var Uniqueid = $('#uniqueid').val();
    
                $.ajax({
                    url: '/Admin/GetUserListBox?SelectedValue=' + SelectedListValue + '&Uniqueid=' + Uniqueid,
                    data: { value: SelectedObjvalue },
                    type: 'GET',
                    async: false,
                    success: function (response) {
                        if (response.length > 0) {
                            $('#lstValue').html('');
                            var options = '';                            
                            $.each(response, function (i, obj) {
                                options += '<option value="' + obj.Value + '">' + obj.Text + '</option>';
                            });
                            $('#lstValue').append(options);
                            $('#lstValue').val(SelectedListValue);
                            $('#lstValue').trigger("chosen:updated");
                            object.value = SelectedObjvalue;
                        }
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        //jAlert("Error. Please, check the data.", " Deactivate User");
                        alert(error.StatusText);
                    }
                });
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题