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">×</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) returnbeforeCreateToken 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">×</a>')is never reached. So this is actually thé way to cancel the creation.
更多推荐
发布评论