<bdo id='IkInt'></bdo><ul id='IkInt'></ul>

        <i id='IkInt'><tr id='IkInt'><dt id='IkInt'><q id='IkInt'><span id='IkInt'><b id='IkInt'><form id='IkInt'><ins id='IkInt'></ins><ul id='IkInt'></ul><sub id='IkInt'></sub></form><legend id='IkInt'></legend><bdo id='IkInt'><pre id='IkInt'><center id='IkInt'></center></pre></bdo></b><th id='IkInt'></th></span></q></dt></tr></i><div id='IkInt'><tfoot id='IkInt'></tfoot><dl id='IkInt'><fieldset id='IkInt'></fieldset></dl></div>
      1. <small id='IkInt'></small><noframes id='IkInt'>

        <tfoot id='IkInt'></tfoot>
        <legend id='IkInt'><style id='IkInt'><dir id='IkInt'><q id='IkInt'></q></dir></style></legend>

        Select2 限制标签数量

        时间:2023-07-31
        <tfoot id='4fkgx'></tfoot>

          <tbody id='4fkgx'></tbody>
        • <bdo id='4fkgx'></bdo><ul id='4fkgx'></ul>

            • <i id='4fkgx'><tr id='4fkgx'><dt id='4fkgx'><q id='4fkgx'><span id='4fkgx'><b id='4fkgx'><form id='4fkgx'><ins id='4fkgx'></ins><ul id='4fkgx'></ul><sub id='4fkgx'></sub></form><legend id='4fkgx'></legend><bdo id='4fkgx'><pre id='4fkgx'><center id='4fkgx'></center></pre></bdo></b><th id='4fkgx'></th></span></q></dt></tr></i><div id='4fkgx'><tfoot id='4fkgx'></tfoot><dl id='4fkgx'><fieldset id='4fkgx'></fieldset></dl></div>
            • <small id='4fkgx'></small><noframes id='4fkgx'>

              <legend id='4fkgx'><style id='4fkgx'><dir id='4fkgx'><q id='4fkgx'></q></dir></style></legend>

                  本文介绍了Select2 限制标签数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有没有办法限制用户可以使用 Select2 添加到输入字段的标签数量?

                  我有:

                  $('#tags').select2({containerCssClass: 'supplierTags',placeholder: "通常的供应商...",最小输入长度:2,多个:真,标记分隔符:[",", " "],placeholder: '通常的供应商...',createSearchChoice:函数(术语,数据){if ($(data).filter(function() {返回 this.name.localeCompare(term) === 0;}).length === 0) {返回{id:0,名称:术语};}},id:函数(e){返回 e.id + ":" + e.name;},阿贾克斯:{url: ROOT + '调用',数据类型:'json',类型:'POST',数据:功能(术语,页面){返回 {call: 'Helpers->tagsHelper',问:术语};},结果:函数(数据,页面){返回 {结果:data.tags};}},格式结果:格式结果,格式选择:格式选择,初始化选择:函数(元素,回调){变量数据 = [];$(element.val().split(",")).each(function(i) {var item = this.split(':');数据.push({id:项目[0],名称:项目[1]});});回调(数据);}});

                  如果可以有一个像 limit: 5 这样的简单参数以及达到限制时触发的回调,那就太好了.

                  解决方案

                  当然,maximumSelectionLength 像这样:

                  $("#tags").select2({最大选择长度:3});

                  <块引用>

                  最大选择长度

                  Select2 允许开发者限制可以选择的项目数量在多选控件中选择.

                  http://ivaynberg.github.io/select2/

                  它没有原生回调,但你可以像这样向 formatSelectionTooBig 传递一个函数:

                  $(function () {$("#tags").select2({最大选择长度:3,formatSelectionTooBig:函数(限制){//打回来return '选择的项目太多';}});});

                  http://jsfiddle.net/U98V7/

                  或者您可以像这样扩展 formatSelectionTooBig:

                  $(function () {$.extend($.fn.select2.defaults, {formatSelectionTooBig:函数(限制){//打回来return '选择的项目太多';}});$("#tags").select2({最大选择长度:3});});

                  编辑

                  maximumSelectionSize 替换为更新后的 maximumSelectionLength.谢谢@DrewKennedy!

                  Is there a way to limit the number of tags a user can add to an input field using Select2?

                  I have:

                  $('#tags').select2({
                      containerCssClass: 'supplierTags',
                      placeholder: "Usual suppliers...",
                      minimumInputLength: 2,
                      multiple: true,
                      tokenSeparators: [",", " "],
                      placeholder: 'Usual suppliers...',
                              createSearchChoice: function(term, data) {
                                  if ($(data).filter(function() {
                                      return this.name.localeCompare(term) === 0;
                                  }).length === 0) {
                                      return {id: 0, name: term};
                                  }
                  
                              },
                      id: function(e) {
                          return e.id + ":" + e.name;
                      },
                      ajax: {
                          url: ROOT + 'Call',
                          dataType: 'json',
                          type: 'POST',
                          data: function(term, page) {
                              return {
                                  call: 'Helpers->tagsHelper',
                                  q: term
                              };
                          },
                          results: function(data, page) {
                              return {
                                  results: data.tags
                              };
                          }
                      },
                      formatResult: formatResult,
                      formatSelection: formatSelection,
                      initSelection: function(element, callback) {
                          var data = [];
                          $(element.val().split(",")).each(function(i) {
                              var item = this.split(':');
                              data.push({
                                  id: item[0],
                                  name: item[1]
                              });
                          });
                          callback(data);
                      }
                  });
                  

                  It would be great if there could be/is a simple parameter like limit: 5 and a callback to fire when the limit is reached.

                  解决方案

                  Sure, with maximumSelectionLength like so:

                  $("#tags").select2({
                      maximumSelectionLength: 3
                  });
                  

                  Maximum Selection Length

                  Select2 allows the developer to limit the number of items that can be selected in a multi-select control.

                  http://ivaynberg.github.io/select2/

                  It has no native callback, but you can pass a function to formatSelectionTooBig like this:

                  $(function () {
                      $("#tags").select2({
                          maximumSelectionLength: 3,
                          formatSelectionTooBig: function (limit) {
                  
                              // Callback
                  
                              return 'Too many selected items';
                          }
                      });
                  });
                  

                  http://jsfiddle.net/U98V7/

                  Or you could extend formatSelectionTooBig like this:

                  $(function () {
                      $.extend($.fn.select2.defaults, {
                          formatSelectionTooBig: function (limit) {
                  
                              // Callback
                  
                              return 'Too many selected items';
                          }
                      });
                  
                      $("#tags").select2({
                          maximumSelectionLength: 3
                      });
                  });
                  

                  Edit

                  Replaced maximumSelectionSize with the updated maximumSelectionLength. Thanks @DrewKennedy!

                  这篇关于Select2 限制标签数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:什么是&lt;dl&gt;标记为? 下一篇:如何制作缩略图&lt;img&gt;单击时显示全尺寸图像?

                  相关文章

                    <small id='SQWN8'></small><noframes id='SQWN8'>

                    <i id='SQWN8'><tr id='SQWN8'><dt id='SQWN8'><q id='SQWN8'><span id='SQWN8'><b id='SQWN8'><form id='SQWN8'><ins id='SQWN8'></ins><ul id='SQWN8'></ul><sub id='SQWN8'></sub></form><legend id='SQWN8'></legend><bdo id='SQWN8'><pre id='SQWN8'><center id='SQWN8'></center></pre></bdo></b><th id='SQWN8'></th></span></q></dt></tr></i><div id='SQWN8'><tfoot id='SQWN8'></tfoot><dl id='SQWN8'><fieldset id='SQWN8'></fieldset></dl></div>

                    1. <tfoot id='SQWN8'></tfoot>
                      <legend id='SQWN8'><style id='SQWN8'><dir id='SQWN8'><q id='SQWN8'></q></dir></style></legend>
                      • <bdo id='SQWN8'></bdo><ul id='SQWN8'></ul>