15

我正在使用选择的插件

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

$(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");
    });
});

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

4

7 回答 7

28

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

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/

于 2013-09-09T21:29:09.707 回答
13

我修改了 Bogdan 的答案以使用所选的 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");
    }
});
于 2014-12-12T14:28:59.723 回答
1

一个简单的替代方法是使用 select2,它内置了标签: https ://select2.github.io/examples.html#tags

$(".js-example-tags").select2({
  tags: true
})
于 2016-06-28T06:25:03.050 回答
0

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

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

基本上,您编辑 selected.jquery.js 文件并搜索case 13:并替换 switch case 中的代码

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;

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

这可以通过将 switch case 中的代码替换为:

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;

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

于 2016-02-24T14:36:44.273 回答
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;
      }
    };
于 2020-04-12T06:18:32.743 回答
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");
于 2021-06-09T20:33:06.470 回答
-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");
});

于 2014-03-25T05:06:32.937 回答