Twitter引导令牌字段取消.on('beforeCreateToken')(Twitter bootstrap tokenfield cancel .on('befo

编程入门 行业动态 更新时间:2024-10-28 09:17:08
Twitter引导令牌字段取消.on('beforeCreateToken')(Twitter bootstrap tokenfield cancel .on('beforeCreateToken'))

Twitter引导令牌字段

如果检测到无效输入,是否有办法取消在.on('beforeCreateToken')内创建令牌?

Twitter bootstrap tokenfield

Is there a way to cancel the creation of a token inside .on('beforeCreateToken') if invalid input is detected?

最满意答案

是。 我假设你正在使用这个库 - > http://sliptree.github.io/bootstrap-tokenfield/ ??

只需在beforeCreateToken处理程序中重置令牌,此处如果要插入的令牌包含在排除列表中:

var exclusionList = ['a','b','c'];

$('#tokenfield').on('beforeCreateToken', function(e) {
  var token = e.token.value;
  if (exclusionList.indexOf(token)>-1) {
    e.token=false;
  } 
});
 

如果为空,则不插入令牌(而是obvoius :)


从源头上看,最新开发版本v0.11.0中的第176-178行( https://github.com/sliptree/bootstrap-tokenfield/blob/master/js/bootstrap-tokenfield.js ):

this.$element.trigger( beforeCreateEvent ) if (!beforeCreateEvent.token) return

在createToken()内部触发beforeCreateToken,如果将e.token设置为false或为空,则退出该函数并且

var token = $('<div class="token" />') .attr('data-value', value) .append('<span class="token-label" />') .append('<a href="#" class="close" tabindex="-1">&times;</a>')

永远不会到达。 所以这实际上是取消创作的方式。

Yes. I assume you are using this library -> http://sliptree.github.io/bootstrap-tokenfield/ ??

Just reset the token in the beforeCreateToken handler, here if the token about to be inserted is contained in a exclusion list :

var exclusionList = ['a','b','c'];

$('#tokenfield').on('beforeCreateToken', function(e) {
  var token = e.token.value;
  if (exclusionList.indexOf(token)>-1) {
    e.token=false;
  } 
});
 

If empty, the token is not being inserted (rather obvoius :)


From the source, about line 176-178 in latest development version v0.11.0 (https://github.com/sliptree/bootstrap-tokenfield/blob/master/js/bootstrap-tokenfield.js) :

this.$element.trigger( beforeCreateEvent ) if (!beforeCreateEvent.token) return

beforeCreateToken is triggered inside createToken(), if you set e.token to false or empty, the function is exited and

var token = $('<div class="token" />') .attr('data-value', value) .append('<span class="token-label" />') .append('<a href="#" class="close" tabindex="-1">&times;</a>')

is never reached. So this is actually thé way to cancel the creation.

更多推荐

本文发布于:2023-08-07 21:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466472.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:令牌   字段   Twitter   beforeCreateToken   cancel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!