使用Chosen插件将选定文本选项以外的文本添加到选择中

时间:2013-09-09 20:45:48

标签: jquery jquery-chosen

我正在使用the chosen plugin

将此添加到我的文档后,我无法输入文本字段并将新项目添加到列表中。

$(document).ready(function (){    
    $(".chosen-select").trigger("chosen:updated");
});

这将允许将一些硬编码文本添加到列表中:

$(document).ready(function (){
    $('.chosen-select').chosen();
    $('.addsomething').click(function (){
        $(".chosen-select").prepend("<option>Utopia</option>");
        $(".chosen-select").trigger("chosen:updated");
    });
});

我希望看到的是我希望能够在文本字段中添加文本,点击返回并在我没有选择的情况下添加我的条目。它不需要永久添加到选项列表中。

7 个答案:

答案 0 :(得分:27)

不确定是否可以更简单的方式完成,但这很好用。代码中的注释解释了每一步:

var select, chosen;

// Cache the select element as we'll be using it a few times
select = $(".chosen-select");

// Init the chosen plugin
select.chosen({ no_results_text: 'Press Enter to add new entry:' });

// Get the chosen object
chosen = select.data('chosen');

// Bind the keyup event to the search box input
chosen.dropdown.find('input').on('keyup', function(e)
{
    // If we hit Enter and the results list is empty (no matches) add the option
    if (e.which === 13 && chosen.dropdown.find('li.no-results').length > 0)
    {
        var option = $("<option>").val(this.value).text(this.value);

        // Add the new option
        select.prepend(option);
        // Automatically select it
        select.find(option).prop('selected', true);
        // Trigger the update
        select.trigger("chosen:updated");
    }
});

以下是一个有效的例子:http://jsfiddle.net/jHvmg/436/

答案 1 :(得分:13)

我修改了Bogdan的the answer以使用选定的1.2.0以及所有类型的选择:

var select, chosen;

// cache the select element as we'll be using it a few times
select = $(".chosen-select");

// init the chosen plugin
select.chosen();

// get the chosen object (any type, single or multiple)
chosen = $('.chosen-container');
// chosen = $('.chosen-select-single');
// chosen = $('.chosen-container-multi');

// Bind the keyup event to the search box input
chosen.find('input').keyup( function(e)
{
    // if we hit Enter and the results list is empty (no matches) add the option
    if (e.which === 13 && chosen.find('li.no-results').length > 0)
    {
        var option = $("<option>").val(this.value).text(this.value);

        // add the new option
        select.prepend(option);
        // automatically select it
        select.find(option).prop('selected', true);
        // trigger the update
        select.trigger("chosen:updated");
    }
});

答案 2 :(得分:1)

一个简单的替代方法是使用select2,其中包含标记: docs

$(".js-example-tags").select2({
  tags: true
})

答案 3 :(得分:0)

这里有一篇文章如何在这里为选定的选择列表添加新值:

https://boundlessjourney.wordpress.com/2014/06/12/adding-new-values-to-chosen-plugin/

基本上你编辑selected.jquery.js文件并搜索case 13:并用

替换开关案例中的代码
case 13:
 evt.preventDefault();
 if (this.results_showing) {
   if (!this.is_multiple || this.result_highlight) {
     return this.result_select(evt);
   }
   $(this.form_field).append('<option>' + $(evt.target).val() + '</option>');
   $(this.form_field).trigger('chosen:updated');
   this.result_highlight = this.search_results.find('li.active-result').last();
   return this.result_select(evt);
  }
  break;

这适用于多选,但就我而言,我希望能够添加一个选项。我在评论中找到了答案,但缺少代码,我添加了将每个单词大写的能力。

这可以通过用以下代码替换开关盒中的代码来完成:

case 13:
  evt.preventDefault();
  if (this.results_showing) {

    if (this.result_highlight) {
      return this.result_select(evt);
    }
    $(this.form_field).append('<option>' + ($(evt.target).val()).replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}) + '</option>');
    $(this.form_field).trigger('chosen:updated');
    this.result_highlight = this.search_results.find('li.active-result').last();
    return this.result_select(evt);
  }
  break;

也许可以编辑代码,因此它可以接受多选和单

答案 4 :(得分:0)

chosen.jquery.js功能的AbstractChosen.prototype.choice_label文件中,您可以更改所选文本

AbstractChosen.prototype.choice_label = function(item) {
      if (this.include_group_label_in_selected && (item.group_label != null)) {
        return "<b class='group-name'>" + (this.escape_html(item.group_label)) + "</b>" + item.html;
      } else {
          return item.html.replace(/ *\([^)]*\) */g, "");;//Ashkufaraz Remove Post From Selected Text
      //  return item.html;
      }
    };

答案 5 :(得分:0)

简单代码

    var VAL = 1;
    var NM = "Aladein";
    $("#YourID").append('<option value="' + VAL + '">' + NM + '</option>');
    $("#YourID option:selected").val(VAL);
    $("#YourID option:selected").text(NM);
    $('#YourID').trigger("chosen:updated");

答案 6 :(得分:-2)

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

$('.choose').on('chosen:no_results',function(evt, params) {
var value = params.chosen.search_results.find('span').text();
$('.choose').append(new Option(value, value,true).outerHTML);
$('.choose').trigger("chosen:updated");
});