allow new values with chosen.js multiple select

后端 未结 10 1969
囚心锁ツ
囚心锁ツ 2020-12-12 19:30

I\'m using the chosen.js plugin http://harvesthq.github.com/chosen/ with jQuery to allow the user to select multiple options from a select. However, I now want to be able to

10条回答
  •  爱一瞬间的悲伤
    2020-12-12 19:55

    You could just attach an event to the input text box to listen for a particular character code. After that add the option and trigger the update on the dropdown.

     var dropDown = $('select.chosen');
     dropDown.parent().find('.chzn-container .chzn-search input[type=text]').keydown( function (evt) {
               var stroke, _ref, target, list;
               // get keycode
               stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
               target = $(evt.target);               
               // get the list of current options
               list = target.parents('.chzn-container').find('.chzn-choices li.search-choice > span').map(function () { return $(this).text(); }).get();
               if (stroke === 9 || stroke === 13) {
                  var value = $.trim(target.val());
                  // if the option does not exists
                  if ($.inArray(value,list) < 0) {
                     var option = $('

提交回复
热议问题