我正在使用带有不错标签输入的模板,但是我现在希望能够计算单词数并根据某些标准限制单词数量我发现这样做的唯一方法是使用特定的TextBoxfor的键,但是要这样做,我需要参考JQuery,但随后它打破了我的标签输入。以下是我目前拥有的:
@Html.TextBoxFor(model => model.EmploymentSkills, new { @id = "tags", @class = "form-control tagsinput", @type = "text", @placeholder = "Add skill and press enter", data_role = "tagsinput" })
<script src="~/SmartAdmin/scripts/plugin/bootstrap-tags/bootstrap-tagsinput.min.js"></script>
有没有办法通过引用jQuery或能够引用它并且不破坏我的标签输入?
data_role
属性是否有特定的东西?只要我记得您应该使用xpath,就应该将所有属性放在 @中。尝试将data_role = 'tagsinput'
替换为@data-role = 'tagsinput'
。也许这是因为引起了问题。
,所以我找到了解决方案。如上所述,.keyup或.keydown无效,所以我使用.change降落。这是我使用的功能,还计算单词数并限制它们:
$(document).ready(function(){
$(".tagsinput").change(function (e) {
var value = $(this).val().replace(" ", "");
var words = value.split(",");
if (words.length > '@Model.TagsAllowed') {
alert("You are not allowed more than '@Model.TagsAllowed' tags!");
var lastword = value.split(",").pop();
value = value.replace(',' + lastword, '');
$(this).val(value);
$('.tagsinput').val() = value.substring(0, value.length - lastword.length);
}
});
})
希望这对沿线的其他任何人都有帮助。