简体   繁体   English

在使用引导标签输入将其转换为标签之前处理标签

[英]process tag before converting it into tag using bootstrap tags input

i am using bootstrap tags input in my site.我在我的网站中使用引导标签输入。 basically what i am trying to do is, ask user to type urls into a text field, now if the text is valid url then only convert it into tag otherwise don't.基本上我想做的是,要求用户在文本字段中键入 url,现在如果文本是有效的 url,则只将其转换为标签,否则不要。

is there any way to process text before converting into tags?有没有办法在转换成标签之前处理文本?

any help would be appriciated.任何帮助将被appriciated。

Thanks谢谢

Bootstrap tags have beforeItemAdd event which triggered just before an item gets added. Bootstrap 标签具有beforeItemAdd事件,该事件在添加项目之前触发。 Bootstrap tags 引导标签

$('input').on('beforeItemAdd', function(event) {
    /* Validate url */
    if (/^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})).?)(?::\d{2,5})?(?:[/?#]\S*)?$/i.test(event.item)) {
        event.cancel = false;
    } else {
        event.cancel = true;
    }
});

Bootstrap Tags Input have a hidden input before convert to tags you find it by following code: Bootstrap 标签输入在转换为tags之前有一个hidden输入,您可以通过以下代码找到它:

$(".bootstrap-tagsinput input[type="text"]").keyup(function(){
// do your validation
});

you can see it by get an inspect element in your page.您可以通过在页面中获取一个inspect element来查看它。 it will convert to tags after you press enter button you can do your validation until it's on hidden input.按下enter button后,它将转换为tags ,您可以进行验证,直到它处于隐藏输入状态。

It will be there:它将在那里:

<div class="bootstrap-tagsinput">
<span class="tag label label-info">Test<span data-role="remove"></span></span> 
<input type="text" style="width: 7em;" size="1"> // here is!
</div>

If your input id is "tag-input", all you have to do is use a beforeItemAdd callback provided by the library itself like so:如果您的输入 id 是“tag-input”,您所要做的就是使用库本身提供的beforeItemAdd回调,如下所示:

$('#tag-input').on('beforeItemAdd', function(event) {
        var tag = event.item;

        if(true){ //if tag is not a url or process other text conditions.
          event.cancel = true
        }
});

If you don't set event.cancel to false, the add will go through.如果您没有将event.cancel设置为 false,则add将通过。

You can check the documentation on this method here .您可以在此处查看有关此方法的文档。

Also , as it is clear in this case that documentation does not say how to cancel the event.此外,很明显,在这种情况下,文档没有说明如何取消事件。 In such cases its simple enough to just check the code itself.在这种情况下,只需检查代码本身就足够简单了。 I this case this code in the plugins github repo makes it plenty clear how to use this option.在这种情况下,插件 github 存储库中的这段代码清楚地说明了如何使用此选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM