How to use Bootstrap Tags Input plugin

前端 未结 2 1001
日久生厌
日久生厌 2021-01-02 06:37

I am trying to use Bootstrap Tags Input plugin without Typeahead. I have included following files:



        
2条回答
  •  情歌与酒
    2021-01-02 07:19

    This solution does not use the plugin but worked even better for me.

    $(function() {
    	$('#form-tags-1').tagsInput();
    
    	$('#form-tags-2').tagsInput({
    		'onAddTag': function(input, value) {
    			console.log('tag added', input, value);
    		},
    		'onRemoveTag': function(input, value) {
    			console.log('tag removed', input, value);
    		},
    		'onChange': function(input, value) {
    			console.log('change triggered', input, value);
    		}
    	});
    
    	$('#form-tags-3').tagsInput({
    		'unique': true,
    		'minChars': 2,
    		'maxChars': 10,
    		'limit': 5,
    		'validationPattern': new RegExp('^[a-zA-Z]+$')
    	});
    
    	$('#form-tags-4').tagsInput({
    		'autocomplete': {
    			source: [
    				'apple',
    				'banana',
    				'orange',
    				'pizza'
    			]
    		}
    	});
    
    	$('#form-tags-5').tagsInput({
    		'delimiter': ';'
    	});
    
    	$('#form-tags-6').tagsInput({
    		'delimiter': [',', ';']
    	});
    });
    
    
    
    /* jQuery Tags Input Revisited Plugin
     *
     * Copyright (c) Krzysztof Rusnarczyk
     * Licensed under the MIT license */
    
    (function($) {
    	var delimiter = [];
    	var inputSettings = [];
    	var callbacks = [];
    
    	$.fn.addTag = function(value, options) {
    		options = jQuery.extend({
    			focus: false,
    			callback: true
    		}, options);
    		
    		this.each(function() {
    			var id = $(this).attr('id');
    
    			var tagslist = $(this).val().split(_getDelimiter(delimiter[id]));
    			if (tagslist[0] === '') tagslist = [];
    
    			value = jQuery.trim(value);
    			
    			if ((inputSettings[id].unique && $(this).tagExist(value)) || !_validateTag(value, inputSettings[id], tagslist, delimiter[id])) {
    				$('#' + id + '_tag').addClass('error');
    				return false;
    			}
    			
    			$('', {class: 'tag'}).append(
    				$('', {class: 'tag-text'}).text(value),
    				$('
    *{box-sizing: border-box;}
    html{height: 100%;margin: 0;}
    body{min-height: 100%;font-family: 'Roboto';margin: 0;background-color: #fafafa;}
    .container { margin: 150px auto; max-width: 960px;}
    label{display: block;padding: 20px 0 5px 0;}
    .tagsinput,.tagsinput *{box-sizing:border-box}
    .tagsinput{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-wrap:wrap;-ms-flex-wrap:wrap;flex-wrap:wrap;background:#fff;font-family:sans-serif;font-size:14px;line-height:20px;color:#556270;padding:5px 5px 0;border:1px solid #e6e6e6;border-radius:2px}
    .tagsinput.focus{border-color:#ccc}
    .tagsinput .tag{position:relative;background:#556270;display:block;max-width:100%;word-wrap:break-word;color:#fff;padding:5px 30px 5px 5px;border-radius:2px;margin:0 5px 5px 0}
    .tagsinput .tag .tag-remove{position:absolute;background:0 0;display:block;width:30px;height:30px;top:0;right:0;cursor:pointer;text-decoration:none;text-align:center;color:#ff6b6b;line-height:30px;padding:0;border:0}
    .tagsinput .tag .tag-remove:after,.tagsinput .tag .tag-remove:before{background:#ff6b6b;position:absolute;display:block;width:10px;height:2px;top:14px;left:10px;content:''}
    .tagsinput .tag .tag-remove:before{-webkit-transform:rotateZ(45deg);transform:rotateZ(45deg)}
    .tagsinput .tag .tag-remove:after{-webkit-transform:rotateZ(-45deg);transform:rotateZ(-45deg)}
    .tagsinput div{-webkit-box-flex:1;-webkit-flex-grow:1;-ms-flex-positive:1;flex-grow:1}
    .tagsinput div input{background:0 0;display:block;width:100%;font-size:14px;line-height:20px;padding:5px;border:0;margin:0 5px 5px 0}
    .tagsinput div input.error{color:#ff6b6b}
    .tagsinput div input::-ms-clear{display:none}
    .tagsinput div input::-webkit-input-placeholder{color:#ccc;opacity:1}
    .tagsinput div input:-moz-placeholder{color:#ccc;opacity:1}
    .tagsinput div input::-moz-placeholder{color:#ccc;opacity:1}
    .tagsinput div input:-ms-input-placeholder{color:#ccc;opacity:1}
    
    
    	
    	    Responsive Tags Input
            
    		
    		
        
    
    
        
        
            

    Responsive Tags Input With Autocomplete Examples

    Original post: https://bootsnipp.com/snippets/exqd3

提交回复
热议问题