首页 新闻 会员 周边

jquery autocomplete插件的问题

1
悬赏园豆:10 [已解决问题] 解决于 2010-12-15 19:19

先看看,我下面的代码吧,我的意思是利用autocomplete插件向aspx读取数据,可无法完成:

代码如下:前台代码:

1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2  <html xmlns="http://www.w3.org/1999/xhtml">
3  <head>
4 <title></title>
5 <script src="../script/jquery-1.4.3.js" type="text/javascript"></script>
6 <link href="../css/jquery.autocomplete.css" rel="stylesheet" type="text/css" />
7 <script src="../script/jquery.autocomplete.js" type="text/javascript"></script>
8
9 <style type="text/css">
10
11 </style>
12 <script type="text/javascript">
13 $(function () {
14 $("input").focus(function () {
15
16 /*$.ajax({
17 type: "GET",
18 dataType: "text",
19 url: "complete.aspx",
20 success: function (data, textStatus) {
21 var str = data.split(",");
22
23
24 $("#autocomplete").autocomplete(str);
25 }
26 }) */
27
28 $("#autocomplete").autocomplete(complete.aspx, {
29 max: 50, //列表里的条目数
30 minChars: 0, //自动完成激活之前填入的最小字符
31 width: 400, //提示的宽度,溢出隐藏
32 scrollHeight: 1000, //提示的高度,溢出显示滚动条
33 matchContains: true, //包含匹配,就是data参数里的数据,是否只要包文本框里的数据就显示
34 autoFill: false, //自动填充
35 formatItem: function (row, i, max) {
36
37 var obj = eval("(" + row + ")"); //转换成js对象
38 return obj.Text;
39 },
40 formatMatch: function (row, i, max) {
41 var obj = eval("(" + row + ")"); //转换成js对象
42 return obj.Text;
43 },
44 formatResult: function (row) {
45 var obj = eval("(" + row + ")"); //转换成js对象
46 return obj.Text;
47 }).result(function (event, row, formatted) {
48
49 });
50 });
51
52
53 $("#loading").ajaxStart(function () {
54 $(this).show();
55 });
56 $("#loading").ajaxStop(function () {
57 $(this).hide();
58 });
59
60 })
61
62 </script>
63 </head>
64 <body>
65
66
67 <p>mystring</p>
68 <div id="mystring"></div>
69 <input type="text" id="autocomplete" />
70 <div id="loading" style="display:none">请稍等:</div>
71 </body>
72 </html>

后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Maticsoft.Web.autocomplete
{
public partial class conplete : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strResult = "{text:'Link A', url:'/page1'}\n {text:'Link B', url: '/page2'} ";
Response.Write(strResult);
}
}
}

关键问题是如何像aspx读取数据,其数据格式是什么的?谢谢

wtq的主页 wtq | 初学一级 | 园豆:146
提问于:2010-11-17 20:26
< >
分享
最佳答案
0
收获园豆:1
邀月 | 高人七级 |园豆:25475 | 2010-11-18 10:09
谢谢你的回答,我主要是要实现在jquery中的autocompleta对中文拼音的支持
wtq | 园豆:146 (初学一级) | 2010-11-18 10:36
其他回答(3)
0

直接返回一个json格式的字符串不是更好吗?

[{text:'a',url:'a.html'},{text:'b',url:'b.html'},{text:'c',url:'c.html'}]

收获园豆:3
I,Robot | 园豆:9783 (大侠五级) | 2010-11-17 22:56
你好,谢谢你的回答,我目前遇到的问题是:如何实现输入“w” 就在下拉列宽中显示“王”,可官方的autocomplete没有这功能,请问如何修改,谢谢
支持(0) 反对(0) wtq | 园豆:146 (初学一级) | 2010-11-18 08:50
0

从aspx或者ashx返回json格式的数据,然后在JS中做解析。

[{ID:1},{Name:Jack},{Age:42}]

function JsonToObject(result) {
return eval('(' + result + ')');
}

然后直接读取result.ID result.Name即可。

收获园豆:3
Young Jeezy | 园豆:158 (初学一级) | 2010-11-18 00:11
你好,谢谢你的回答,我目前遇到的问题是:如何实现输入“w” 就在下拉列宽中显示“王”,可官方的autocomplete没有这功能,请问如何修改,谢谢。
支持(0) 反对(0) wtq | 园豆:146 (初学一级) | 2010-11-18 08:43
0

你可以参看我的这遍文章,里面有你需要的功能。

http://www.cnblogs.com/eflylab/archive/2009/09/18/1569043.html

支持多种输入提示方式,拼音,数字,汉字等。这是我之前弄一个股票系统用到的!

收获园豆:3
冯岩 | 园豆:221 (菜鸟二级) | 2010-11-18 09:08
你好,你的那篇文章我看了,可是还有点疑惑else if(typeof(options.reasultSearch)=="function") { var csub = []; $.each(allData, function(i, row) { var ms = options.reasultSearch(row,q); if(ms!=null && ms!=false) { csub.push(ms); } }); return csub; } 一直以来我一直对autocomplete中的参数q很不了解,,以上代码有点模糊。 还有 reasultSearch:function(row,v)//本场数据自定义查询语法 注意这是我自己新加的事件 { //自定义在code或spell中匹配 if(row.data.code.indexOf(v) == 0 || row.data.spell.indexOf(v) == 0) { return row; } else return false; } }); 这代码row.data.code.indexOf(v) == 0 如果要实现拼音搜索汉字时,只要把row.data.code.indexOf(v) == 0 该成row.data.name.indexOf(v) == 0 就可以了,这原理我不太懂,你能解释一下吗?谢谢
支持(0) 反对(0) wtq | 园豆:146 (初学一级) | 2010-11-18 09:20
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册