2016-02-06 2 views
0

Кто-нибудь знает, как мы можем установить максимальное количество длин тега в select2. Ниже мой код, и в этом коде я могу создать длинный тег (15-20). Я хочу ограничить его до 10 символов.jquery select2: установить длину тега при создании тега

<script type="text/javascript"> 
var lastResults = []; 

$("#tags").select2({ 
    multiple: true, 

    //tags: true,  
    placeholder: "Please enter tags", 
    tokenSeparators: [","], 
    initSelection : function (element, callback) { 
     var data = []; 
     $(element.val().split(",")).each(function() { 
      data.push({id: this, text: this}); 
     }); 
     callback(data); 
    }, 
    ajax: { 
     multiple: true, 
     url: "fetch.php", 
     dataType: "json", 
     delay: 250, 
     type: "POST", 
     data: function(term,page) { 
         return {q: term}; 
        }, 
        results: function(data,page) { 
         return {results: data}; 
        }, 

    }, 
    minimumInputLength: 2, 
     // max tags is 3 
    maximumSelectionSize: 3, 
    createSearchChoice: function (term) { 
     var text = term + (lastResults.some(function(r) { return r.text == term }) ? "" : " (new)"); 
     // return { id: term, text: text }; 
     return { 
      id: $.trim(term), 
      text: $.trim(term) + ' (new tag)' 
     };   
    }, 
}); 

$('#tags').on("change", function(e){ 
    if (e.added) { 
     if (/ \(new\)$/.test(e.added.text)) { 
      var response = confirm("Do you want to add the new tag "+e.added.id+"?"); 
      if (response == true) { 
       alert("Will now send new tag to server: " + e.added.id); 
       /* 
       $.ajax({ 
        type: "POST", 
        url: '/someurl&action=addTag', 
        data: {id: e.added.id, action: add},  
        error: function() { 
         alert("error"); 
        } 
       }); 
       */ 
      } else { 
       console.log("Removing the tag"); 
       var selectedTags = $("#tags").select2("val"); 
       var index = selectedTags.indexOf(e.added.id); 
       selectedTags.splice(index,1); 
       if (selectedTags.length == 0) { 
        $("#tags").select2("val",""); 
       } else { 
        $("#tags").select2("val",selectedTags); 
       } 
      } 
     } 
    } 
}); 
</script> 

Я не ищу minimumInputLength или maximumSelectionSize.

ответ

0

Похоже, вы ищете опцию maximumInputLength. Это ограничит длину, разрешенную для пользовательских тегов и при поиске.

+0

Я не ищу для поиска. Я ищу создание нового тега. – Ironic

Смежные вопросы