Jquery Chosen plugin - dynamically populate list by Ajax

后端 未结 12 1039
野性不改
野性不改 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 11:48

    The chosen answer is outdated, same goes to meltingice /ajax-chosen plugin.

    With Select2 plugin got many bugs which is i can't resolve it.

    Here my answer for this question.

    I integrated my solution with function trigger after user type. Thanks to this answer : https://stackoverflow.com/a/5926782/4319179.

    //setup before functions
      var typingTimer;                //timer identifier
      var doneTypingInterval = 2000;  //time in ms (2 seconds)
      var selectID = 'YourSelectId';    //Hold select id
      var selectData = [];           // data for unique id array
    
      //on keyup, start the countdown
      $('#' + selectID + '_chosen .chosen-choices input').keyup(function(){
    
          // Change No Result Match text to Searching.
          $('#' + selectID + '_chosen .no-results').html('Searching = "'+ $('#' + selectID + '_chosen .chosen-choices input').val() + '"');
    
    
          clearTimeout(typingTimer);  //Refresh Timer on keyup 
          if ($('#' + selectID + '_chosen .chosen-choices input').val()) {
    
               typingTimer = setTimeout(doneTyping, doneTypingInterval);  //Set timer back if got value on input
    
          }
    
      });
    
      //user is "finished typing," do something
      function doneTyping () {
    
          var inputData = $('#' + selectID + '_chosen .chosen-choices input').val();  //get input data
    
          $.ajax({
            url: "YourUrl",
             data: {data: inputData},
            type:'POST',
            dataType: "json",
            beforeSend: function(){
              // Change No Result Match to Getting Data beforesend
              $('#' + selectID + '_chosen .no-results').html('Getting Data = "'+$('#' + selectID + '_chosen .chosen-choices input').val()+'"');
        },
            success: function( data ) { 
    
              // iterate data before append
              $.map( data, function( item ) {
    
                // matching data eg: by id or something unique; if data match: ');
    
                }            
    
              });
    
              // Update chosen again after append 

提交回复
热议问题