One of the plug-ins is the development of jquery's tag function to enhance the production or deletion of tags is very useful, but also to the input of duplicate labels to check, with the jquery AutoComplete plug-ins to achieve the automatic completion function. Official website: http://xoxco.com/projects/code/tagsinput/using the following methods
Introduced
<link href= ". /.. /resources/css/jquery.tagsinput.css "rel=" stylesheet "type=" Text/css "/>
General introduction <script type=" text/ JavaScript "src=". /.. /resources/js/jquery.mytagsinput.js "></script>
Here I made some changes to the file address at the end of the article.
Javascript
<script type= "Text/javascript" > /*** * @Author sonet * If you want to change the tags configuration, please go to jquery.tagsinput.js to change **/ //add tags function onaddtag (tag) { $.mpbalert ({//mpbalert Here for its own tool class, replaceable to its own pop-up layer & Nbsp; content: "OK Add" +tag, icon: "Question", ok : function () { //add tags $.mpbajax ( "/admin/job/addjobtypes", { data:{ &Nbsp; _method: "Put", tagname:tag }, async:false, success:function (data) {
loaddata ();
Location.reload (); } }
); //end add tags &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}, cancel : function () {
$ ("#tags"). Removetag (tag); }
}); } //remove tags function onremovetag (tag) { $.mpbalert ({&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp; content: "OK delete" +tag, icon: "Question", ok : function () { //delete tags $.mpbajax ( "/ Admin/job/removejobtypes ",
{ data:{ _method: "DELETE", tagname:tag }, &Nbsp; async:false, success:function (data) {
loaddata (); } } ); //delete add tags &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}, cancel : function () { $ ("#tags
"). Addtag (tag); }
}); } //change tags function onchangetag (Input,tag) { alert ("Changed a
tag: " + tag); }
//tags controller $ (function () {
loaddata ();
$ ("Span .tag"). Click (function () { alert ("Adsdad"
);
}); $ ("#tags"). Tagsinput ({ &nbsP; width: ' Auto ', onaddtag:function (tag) {
onaddtag (tag); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP},
onremovetag:function (TAG) {
onremovetag (tag); } //, // interactive:false / /no additional label });
}); function loaddata () { $.mpbajax ( "/admin/job/getalljobtypes", { data:{ _method: "Get" }, &nbsP; async:false, success: function (data) {//Scrabble string for tag display
var strs= ""; for (var i in data) {
strs+=data[i].name+ ","; } &nbsP; strs=strs.substring (0,strs.length-1)
;
$ ("#tags"). attr ("value", STRs); } }
); } //edit tags function
edittags (value) { $.mpbalert ({ content: "OK modified to <input type=\" text\ " id=\" Edittags\ "" "+ Value+ ">" icon: "", Ok : function () {
var newtag = $ ("#editTags"). Val (); $.mpbajax ( "/admin/job/updatejobtypes ", { data:{ _method: "POST", oldtag:value, newtag:newtag }, async:false, success:function (data) {
location.reload (); } }
); &NBSP;&NBSP;&NBSP}, cancel : function () { } }
); } function addnewtag () {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp; var str = $ ("#addNewTag"). Val (); if ($.isnotblank (str)) {
onaddtag (str); } } </script>
Xx.html>body
The value taken from the background will be processed by JS to "," the split string assigned to the ID tags of the input label value attribute.
For example: A,b,c
<body>
Please enter a job Category: <input type= "text" id= "Addnewtag" value= ""/><input type= "Submit" onclick= " Addnewtag (); "value=" Add "/>
<form>
<p><label> position category Management:</label></p>
& Lt;input id= "tags" type= "text" class= "tags" value= "a,b,c"/>
</form>
</body>
Jquery.mytagsinput.js
Configuration Area $.fn.tagsinput = function (options) { var settings = Jquery.extend ({ interactive:true, //Interactive defaulttext: ' Add a category ', //hint language minchars:0, Width: ' 100px ', //editing area Height: ' 300px ', //edit area height AUTOCOMPLETE:&NBSP;{SELECTFIRST:&NBSP;FALSE&NBSP}, ' hide ': true, ' delimiter ': ', ', //separator ' unique ':true, //Unique removewithbackspace:true, placeholdercolor: ' #666666 ', autosize: true, comfortzone: 20, inputpadding: 6*2},options);
The value in the input box of the //html page is split through the following code $.fn.tagsinput.importtags = function (obj,val) {
$ (obj). Val (');
var id = $ (obj). attr (' id ');
var tags = val.split (Delimiter[id]); for (i=0; i<tags.length; i++) {
$ (obj). Addtag (Tags[i],{focus:false,callback:false}); &NBSP;&NBSP;&NBSP;&NBSP} if (Tags_callbacks[id] && tags_callbacks[id)
[' OnChange ']) { var f = tags_callbacks [ID]
[' OnChange '];
f.call (Obj, obj, tags[i]); &NBSP;&NBSP;&NBSP;&NBSP}};
The
Split character calls the Addtag method in turn to add values to the field
Add tags $.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 (Delimiter[id]); if ( tagslist[0] == ') {
tagslist = new array (); }
value = jquery.trim (value); if (options.unique) { var skiptag = $ (
This). Tagexist (value); if (skiptag == true) { //Marks fake Input as not_valid to let styling it $ (' # ' +id+ ' _tag '). AddClass (' not_valid '); } } else { var skipTag = false; } if (value != ' && skiptag != true) { $ (' <span> '). addclass (' tag '). Append ( $ (' <span> '). Text ( Value). Append (' &nbsp;&nbsp; '), $ (' <a> ', { href : ' # ', title : ' Removing tag ', text : ' x ' &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}). Click (function () {
return $ (' # ' + id). Removetag (Escape (value)); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}) ). Click (function () {//add edit funciton &Nbsp; edittags (value),//calling external functions to make changes in the form of a pop-up layer &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}). InsertBefore (' # '
+ id + ' _addtag ');
tagslist.push (value); $ (
' # ' +id+ ' _tag '). Val ('); if (options.focus) { $ (' # ' +id+ ' _tag '). focus (); } else {
$ (' # ' +id+ ' _tag '). blur (); }
$.fn.tagsinput.updatetagsfield (this,tagslist); if (options.callback && tags_callbacks[id] && tags_callbacks[id][' OnAddTag '] {
var f = tags_callbacks[id][' Onaddtag '];
f.call (This, value); } if (tags_callbacks[id] && tags_callbacks[id][' OnChange ') {
var i = tagslist.length;
var f = tags_callbacks[id][' OnChange '];
f.call (this, $ (this), tagslist[i-1]); } } &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP});
return false;
}; //end add tags
JQuery tags Input Plugin (Add/Remove tags plugin)
First, refer to the following two files
<script src= "Jquery.tagsinput.js" ></script> <link rel= "stylesheet" type= "Text/css"
Jquery.tagsinput.css "/>
In your table dropdowns create a input box containing the tags list, you can set the default or existing tags in value, separated by commas.
<input name= "tags" id= "tags" value= "Foo,bar,baz"/>
Then, simply call the Tagsinput () function to any one of the input tags and it will be treated as a tags list
$ (' #tags '). Tagsinput ();
If you want to use Jquery.autocomalete (AutoComplete), add a parameter with a AutoComplete URL in the function.
$ (' #tags '). Tagsinput ({
autocomplete_url: ' Http://myserver.com/api/autocomplete '
});
If you use the Jquery.autocomplete plugin on the bassistance.de site, you can add additional parameters to strengthen the AutoComplete plug-in, as described below.
$ (' #tags '). Tagsinput ({
autocomplete_url: ' Http://myserver.com/api/autocomplete ',
autocomplete:{ Selectfirst:true,width: ' 100px ', Autofill:true}
});
PS: It should be noted that the demo is the default jquery UI AutoComplete, if you want to test jquery.autocomplete, to the head of the annotated reference file to remove the annotation, and the $ (' #tags '). Tagsinput ({ The address of Autocomplete_url is changed to the corresponding Jquery.autocomplete file. Specific look at the page has instructions.
You can also use the Addtag () and Removetag () functions to add and remove tags, such as the following:
$ (' #tags '). Addtag (' foo ');
$ (' #tags '). Removetag (' Bar ');
You can also use the Importags () method to guide a set of tag list, you need to note that this will be the value set in the default tag to replace
$ (' #tags '). Importtags (' Foo,bar,baz ');
So if the importtags () does not have a value, is to reset the label value in input (note that the quotation mark to keep, you can understand that it is to send a null value.) )
$ (' #tags '). Importtags (');
You can use Tagexist () to determine whether a label exists:
if ($ (' #tags '). Tagexist (' foo ')}
If you want to add additional functionality or trigger other actions when adding or removing labels, you can specify a callback in both the Onaddtag and Onremovetag parameters. Both functions return a label value as an argument (original: Both functions should accept a single tag as the parameter.)
$ (' #tags_1 '). Tagsinput ({
width: ' auto ',
onaddtag:function (tag) {
console.log (' added ' +tag)
},
onremovetag:function (tag) {
console.log (' Deleted ' +tag}}
}
);
If you want to ban tagging, or if you want to provide other interactive ways to add labels, you can add a interactive parameter with a value of false, which disables the addition of tags, while other features and rendering are the same.
$ (' #tags_1 '). Tagsinput ({
width: ' auto ',
onremovetag:function (tag) {
console.log (' Remover ' + ' "' +ta G+ ' ")
},
interactive:false
});
If you want to call a function every time you add/remove a label, you can increase the onchange parameter and set the callback function
By default, if the mouse is behind a label, pressing the BACKSPACE key removes the label. If you want to disable this, set the Removewithbackspace argument to False.
Parameters:
$ (selector). Tagsinput ({ ' autocomplete_url ': url_to_autocomplete_api, //automatically completes the plug-in's file address, Demo with instructions ' AutoComplete ': { option: value, option: value}, // Automatic completion of plug-in parameters, demo has instructions. (Provide a jquery.autocomplete: http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/) ' Height ': ' 100px ', //set altitude ' width ': ' 300px ', //set width '
Interactive ': true, //whether to allow tagging, false to block ' defaulttext ': ' Add a tag ', //default text ' Onaddtag ': callback_function, //add tag callback function ' Onremovetag ': callback_
function, //a callback function that deletes a label ' OnChange ' : callback_function, //the callback function when changing a label ' Removewithbackspace ' : true, //whether to allow the BACKSPACE key to be used to remove the preceding label, False to block ' Minchars ' : 0, //each label's small most characters ' maxchars ' : 0 //the maximum character for each label, if not set or 0
, is infinitely big ' Placeholdercolor ' : ' #666666 ' //set DefaultText color});