【发布时间】:2011-06-19 09:21:22
【问题描述】:
我不能向同一个字段添加几个值。我只能选择一个值,并且在输入,、; 或其他分隔符后,我无法选择另一个值。我希望它的工作方式类似于自动完成。
我有一个带有 jQuery 绑定的文本框:
<div class="editor-field">
@Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name)
</div>
<script type="text/javascript">
$(document).ready(function () {
$("#Name").autocomplete('@Url.Action("TagName", "Tag")', {
minChars: 1,
delimiter: /(,|;)\s*/,
onSelect: function(value, data){
alert('You selected: ' + value + ', ' + data);
}
});
});
</script>
它使用来自我的控制器的数据:
public ActionResult TagName(string q)
{
var tags = new List<TagModel>
{
new TagModel {Name = "aaaa", NumberOfUse = "0"},
new TagModel {Name = "mkoh", NumberOfUse = "1"},
new TagModel {Name = "asdf", NumberOfUse = "2"},
new TagModel {Name = "zxcv", NumberOfUse = "3"},
new TagModel {Name = "qwer", NumberOfUse = "4"},
new TagModel {Name = "tyui", NumberOfUse = "5"},
new TagModel {Name = "asdf[", NumberOfUse = "6"},
new TagModel {Name = "mnbv", NumberOfUse = "7"}
};
var tagNames = (from p in tags where p.Name.Contains(q) select p.Name).Distinct().Take(3);
string content = string.Join<string>("\n", tagNames);
return Content(content);
}
我正在使用这些脚本:
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.autocomplete.js")" type="text/javascript"></script>
<link href="@Url.Content("~/Scripts/jquery.autocomplete.css")" rel="stylesheet" type="text/css" />
firebug 没有错误。我的代码有什么问题?
【问题讨论】:
-
jqueryui 自动完成有一个 example 正是要查找的内容。可能比一年多未更新且用户群较小的插件更好。
-
你有权利。但是如果你给我代码来代替 source: function( request, response ) { $.getJSON( "search.php", { term: extractLast( request.term ) }, response );从我的帖子中粘贴解决方案以获取数据,这对我来说很重要。并创建一个答案。我必须你给点:)
-
我用的是自动补全,没问题,你就不能只用一个分隔符,你为什么有这么多?
标签: jquery asp.net-mvc-3 razor