使用此插件 https://github.com/aehlke/tag-it 顺便说一句,这很酷。
问题:
<input type="hidden" name="tags" id="mySingleField" value="Apple, Orange" disabled="true">
Tags:<br>
<ul id="mytags"></ul>
<script type="text/javascript">
$(document).ready(function () {
$("#mytags").tagit({
singleField: true,
singleFieldNode: $('#mySingleField'),
allowSpaces: true,
minLength: 2,
removeConfirmation: true,
tagSource: function (request, response) {
//console.log("1");
$.ajax({
url: "../City/GetList",
data: { term: request.term },
dataType: "json",
success: function (data) {
response($.map(data, function (item) {
return {
label: item.label + " (" + item.id + ")",
value: item.value
}
}));
}
});
}
});
});
</script>
当标记它选择值时,它会在值attr中以CSV格式将值添加到隐藏字段。我想让它做ID而不是任何人都知道如何?
答案 0 :(得分:1)
这里有几件事。您可以通过将参数设置为下划线来将分隔符而不是CSV设置为任何内容:
$("#mytags").tagit({
...
singleFieldDelimiter: '_',
...
然后你可以修改第197行的tag-it.js文件来说明使用ID属性。
变化:
var tags = node.val().split(this.options.singleFieldDelimiter);
要
var tags = node.attr("id").split(this.options.singleFieldDelimiter);
因此,假设您将隐藏字段修改为:
<input type="hidden" name="tags" class="mySingleField" id="Apple_Orange_Banana" value="Apple_Orange" disabled="true">
您可以修改javascript以获得所需的输出:
$(document).ready(function () {
$("#mytags").tagit({
singleField: true,
singleFieldNode: $('.mySingleField'),
singleFieldDelimiter: '_',
allowSpaces: true,
minLength: 2,
removeConfirmation: true,
tagSource: function (request, response) {
//console.log("1");
$.ajax({
url: "../City/GetList",
data: { term: request.term },
dataType: "json",
success: function (data) {
response($.map(data, function (item) {
return {
label: item.label + " (" + item.id + ")",
value: item.value
}
}));
}
});
}
});
});
答案 1 :(得分:0)
change the tag-it.js file
comment from line 264
//that.createTag(that._cleanedInput());
// The autocomplete doesn't close automatically when TAB is pressed.
// So let's ensure that it closes.
//that.tagInput.autocomplete('close');
around line 285
var autocompleteOptions = {
select: function(event, ui) {
that.createTag(ui.item);
Create a new function
assignedTagsData : function(){
// Only to be used when singleField option is not seletced
var tags = [];
this.tagList.children('.tagit-choice').each(function() {
tags.push($(this).data('tag_item_data') );
});
return tags;
}
> that.createTag(ui.item);
// Create tag.
var tag = $('<li></li>')
.data('tag_item_data',item) //add this line
.addClass('tagit-choice ui-widget-content ui-state-default ui-corner-all')
.addClass(additionalClass)
.append(label);