我不能将几个值添加到同一个字段中。只能选择一个值,输入,、;等分隔符后,不能再选。我希望它的工作方式类似于自动完成。
我有一个绑定了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中没有错误。我的代码出了什么问题?

发布于 2011-06-24 06:49:30
遇到了firebug的这种问题。
我建议在Firebug控制台包含错误消息之前不要信任它
如果你的代码没有像预期的那样工作,firebug没有给你显示任何错误信息,那么是时候在chrome中检查你的网页,看看控制台选项卡中到底哪里没有处理异常,特别是当你使用ajax的时候。
发布于 2011-06-19 17:43:20
我建议你使用更新的jQuery UI autocomplete插件。jQuery ui 1.8甚至是作为新的ASP.NET MVC3项目的一部分分发的。
就你的代码而言,尝试像这样修复它:
var url = '@Url.Action("TagName", "Tag")';
$('#Name').autocomplete(url, {
minChars: 1,
multiple: true,
formatResult: function(row) {
return row[0].replace(/(<.+?>)/gi, '');
}
}).result(function (event, data, formatted) {
alert(!data ? "No match!" : "Selected: " + formatted);
});发布于 2011-06-23 00:59:11
与独立jQuery插件相比,jQuery UI中包含的自动完成功能具有不同的代码格式。有关这方面的详细信息,请访问jQuery UI网站的以下链接。
http://jqueryui.com/demos/autocomplete/
以下是jQuery UI自动完成功能的一个简单示例
$( "#Name" ).autocomplete({
source: url,
minLength: 1,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value );
}
});https://stackoverflow.com/questions/6401594
复制相似问题