我对jquery-ui很新。我想将this与ajax一起使用。 (ASP.NET MVC 5)我创建了jquery-ui-combobox.js并添加了jquery(组合框)函数。
我想这样用。这个论点(来源)是否正确?
<script>
$(function () {
$("#combobox").combobox({source: function (request, response) {
$.ajax({
url: "/Home/GetProjects",
dataType: "json",
data: {
term: request.term
},
type: "POST",
success: function (data) {
/*I dont know what I should here*/
})
);
}
});});
$("#toggle").click(function () {
$("#combobox").toggle();
});
});
</script>
控制器:
public JsonResult GetProjects(string term)
{
var list = new List<SelectListItem>();
list.Add(new SelectListItem { Value = "1", Text = "ActionScript" });
list.Add(new SelectListItem { Value = "2", Text = "AppleScript" });
list.Add(new SelectListItem { Value = "3", Text = "Asp" });
var res = list.Where(p => p.Text.Contains(term));
return Json(res , JsonRequestBehavior.AllowGet);
}
Please help.
答案 0 :(得分:1)
成功你可以这样做......
$("#combobox").append("<option value=''>Select</option>");
$.each(data, function (value, key) {
$("#combobox").append("<option value='"+key.Value+"'>"+key.Text +"</option>");
});