Jan*_*ilu 1 .net c# asp.net jquery jquery-ui
就像在标题中描述的那样,我得到一个小问题,让jqueryUI自动完成小部件工作.
这听起来很傻但是我m hanging the whole day getting that thing solved, but i didn.我开发了几年C#,现在尝试用一个月左右的时间来开发asp和jquery.只是为了展示,我搜索了网络,特别是stackoverflow,并尝试了很多让它运行.
好的,这是代码.
定义TextBox:
<asp:TextBox ID="txtSearchbox"
style="float:left;padding:5px 1px 5px 1px;" runat="server" >
</asp:TextBox>
Run Code Online (Sandbox Code Playgroud)
AutoComplete Jquery脚本部分:
<script type="text/javascript">
$(document).ready(function () {
$('#txtSearchbox').autocomplete( {
source: function (request, response) {
//console.log(request.term);
$.ajax
({
url: "AutoComplete.asmx/GetSearchInfo",
data: "{ 'prefixText': '" + request.term + "' }",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
dataFilter: function (data) {
//console.log(data.toString());
//alert(data.toString());
return data;
},
success: function (data) {
// console.log(data.d.toString());
response($.map(data.d, function (item) {
// console.log(item.Isin)
// console.log(item.Name)
return
{
label: item.Name.toString()
value: item.Name.toString()
}
}));
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
},
minLength: 2
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
AutoComplet.asmx:
[WebMethod]
public List<SearchObject> GetSearchInfo(string prefixText)
{
var seo = new SearchObject();
var getSeo = staticList.getSEOlist().Where(str => str.SearchString.ToLower().Contains(prefixText.ToLower()));
return getSeo.ToList();
}
Run Code Online (Sandbox Code Playgroud)
为了完整起见,CSS:
/*AutoComplete flyout */
.autocomplete_completionListElement
{
margin:0px!important;
background-color:#ffffff;
color:windowtext;
border:buttonshadow;
border-width:1px;
border-style:solid;
cursor:'default';
overflow:auto;
height:200px;
font-family:Tahoma;
font-size:small;
text-align:left;
list-style-type:none;
padding: 5px 5px 5px 5px;
}
/* AutoComplete highlighted item */
.autocomplete_highlightedListItem
{
background-color:#ffff99 ;
color:black;
padding:0px;
}
/* AutoComplete item */
.autocomplete_listItem
{
background-color:window;
color:windowtext;
padding:0px;
}
Run Code Online (Sandbox Code Playgroud)
如果您需要更多,请告诉我.
调试行已取消注释.
如果我检查jquery部分我得到我想要的数据但它不会显示在txtsearch.我真的不明白AutoComplete jquerUI方法将如何显示List,但编码应该是正确的.
实际上,你可能是一个非常讨厌的JavaScript功能的受害者,称为自动分号插入.成功回调/ jQuery映射函数中的return语句写错了.
return
{
label: item.Name.toString()
value: item.Name.toString()
}
Run Code Online (Sandbox Code Playgroud)
这应该是正确的语法:
return {
label: item.Name.toString()
value: item.Name.toString()
}
Run Code Online (Sandbox Code Playgroud)
JavaScript编译器在第一种情况下在return语句后面添加一个自动分号,导致它不返回任何/ undefined.
这个SO问题非常好地概述了这种行为的规则.
| 归档时间: |
|
| 查看次数: |
3110 次 |
| 最近记录: |