我正在尝试使用基于本教程的jQuery UI在我的MVC 5 Web应用程序中实现AutoComplete:http://blogs.msdn.com/b/stuartleeks/archive/2012/04/23/asp-net-mvc-amp-jquery-ui-autocomplete.aspx
以下是我的相关代码:
_Layout.cshtml:
@Styles.Render("~/Content/css")
@Styles.Render("~/Content/jqueryui")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryui")
我已经确认所有这些都正确加载。
Name.cshtml:
<script type="text/javascript" language="javascript">
$(document).ready(function () {
$('#cname').autocomplete({
source: '@Url.Action("Autocomplete")'
});
})
</script>
<form method="get">
<label for="cname">Name:</label>
<input type="text" name="cname" id="cname" />
<input type="Submit" value="Submit" />
</form>
HomeController.cs
public ActionResult Autocomplete (string term)
{
var items = new[] { "John", "Bill", "Steve", "George", "Phil" };
var filteredItems = items.Where(
item => item.IndexOf(term, StringComparison.InvariantCultureIgnoreCase) >= 0
);
return Json(filteredItems, JsonRequestBehavior.AllowGet);
}
当我在Chrome中加载http://localhost:22982/Home/Name
时,我会在控制台中看到这一点:
Uncaught TypeError: undefined is not a function Name:120
(anonymous function) Name:120
a jquery?v=FVs3ACwOLIVInrAl5sdzR2jrCDmVOWFbZMY6g6Q0ulE1:1
h.fireWith jquery?v=FVs3ACwOLIVInrAl5sdzR2jrCDmVOWFbZMY6g6Q0ulE1:1
k jquery?v=FVs3ACwOLIVInrAl5sdzR2jrCDmVOWFbZMY6g6Q0ulE1:1
u jquery?v=FVs3ACwOLIVInrAl5sdzR2jrCDmVOWFbZMY6g6Q0ulE1:1
似乎浏览器甚至从不查看jQueryUI文件,并且从jQuery本身抛出错误。有谁知道为什么会这样?