How to choose the text without selection in jquery chosen plugin

后端 未结 1 1752
孤独总比滥情好
孤独总比滥情好 2021-01-25 23:00

The below is my code my problem is i just want to implement like kendo i mean not with selection when user types the text and if we won\'t select the text it should be selected

1条回答
  •  梦谈多话
    2021-01-25 23:37

    The only way I found to do so is once the dropdown is hidden - check if the value of the input is the same as the value of the first element in the drop down, and if so - trigger the mouseup for that element (inside the dropdown):

    Check live sample:

    $(".chosen-select").chosen();
    $(".chosen-select").bind('chosen:hiding_dropdown', function(e, i) {
      searched_value = i.chosen.get_search_text();
      firstElementOnDropdown = i.chosen.search_results.find('li.active-result').first()
      if (firstElementOnDropdown.text().toLowerCase() == searched_value.toLowerCase()) {
        firstElementOnDropdown.trigger('mouseup');
        var t = i;
        setTimeout(function() {
          t.chosen.input_blur();
        }, 150);
      }
    });
    
    
    
    
    Multiple Select with Groups

    Update

    Added setTimeout to blur the input (which cause the menu to close), since there is another setTimeout inside the code of chosen so needed to hide it after it's shown.

    Explanation: Inside the chosen code there is is a setTimeout function to show the menu. I needed to overcome this behavior so I added a new setTimeout, but with higher interval.

    The setTimeout gets 2 arguments

    • Function to run
    • Time to wait (in milliseconds).

    The function will run after the timeout is up. In my sample - the function is call the input_blur of the chosen menu (to make sure the menu is hidden), and I made sure it's called after 150 milliseconds).

    0 讨论(0)
提交回复
热议问题