我从这里使用tagit:http://aehlke.github.io/tag-it/它会生成一个输入框来标记项目。我需要访问输入框,但它没有 ID。基本上是这样的:
<input type="text" class="ui-widget-content ui-autocomplete-input dirty ui-corner-all" autocomplete="off">
我已经尝试过这样的一切:
alert($('.ui-widget-content ui-autocomplete-input dirty ui-corner-all input[type=text]').val());
但仍然无法访问它...
我尝试在用户键入值时获取该值并将其传递给 Web 服务以执行自动完成:
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/Code/WebServices/Tags.asmx/GetTags",
dataType: "json",
data: '{"entityId":"' + entityId + '","search":"' + NEED_VALUE_HERE + '"}',
success: function(data) {
response($.map(data.d, function(item) {
return {
label: item.TagName,
value: item.TagName
}
}));
}
});
看看我在哪里有 NEED_VALUE_HERE,我需要这里 tag-it 输入框的值...
最佳答案
这段代码取 self 的一个项目:
$("#UL-OF-TAGIT").tagit({
singleField: true,
allowSpaces: true,
removeConfirmation: true,
singleFieldNode: $("#FIELD-TAG-IT"),
tagSource: function( request, response ) {
$.ajax({
url: "YOUR-URL",
data: { term:request.term },
dataType: "json",
success: function( data ) {
response( $.map( data, function( item ) {
return {
label: item.label,
value: item.value
}
}));
}
});
}
});
这是我的带有自动完成功能的标签。
要获取用户正在编写的内容,请使用request.term
。来自文档:
A request object, with a single term property, which refers to the value currently in the text input. For example, if the user enters "new yo" in a city field, the Autocomplete term will equal "new yo".
关于javascript - 访问 tag-it 输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24685773/