2011-04-03 71 views
1

我有一个创建一个GridView的ASPX页面。我的JQuery函数从Gridview数据中提取值来填充我的JQuery自动完成源。所有的工作都很好。现在我想让自动完成值在点击时进入链接。该链接也在Gridview中,我使用text()来获取文本值。 (搜索我的包含“ModelDetail”和存储文本到一个数组链接的GridView)我需要一个jQuery的2维数组?如果是这样,我该怎么做?这里是我的代码: JQuery的,ASP:GridView的和自动完成 - 为自动完成需要链接值

<script language="javascript" type="text/javascript"> 
    $(document).ready(function() { 

     var modelnames = new Array(); 
     $('#ctl00_body_modellistGrid a[href*="ModelDetail"]').each(function() { 
      modelnames.push($(this).text()); 
     }) 


     $("input#autocomplete").autocomplete({ 
      source: modelnames 
     }); 

    }); 

</script> 

提前感谢! 鲍勃

+0

哪个自动完成插件您使用的?这个? http://docs.jquery.com/Plugins/autocomplete – Pandincus 2011-04-03 01:30:08

+0

@Pandincus - 看起来像jQuery UI – arma 2011-04-03 01:31:18

+0

@arma - 哦,我现在看到,原来的插件已弃用,以支持jQuery UI。谢谢! – Pandincus 2011-04-03 01:32:21

回答

1

的JavaScript处理任意对象非常漂亮 - 使用对象存储文本和链接。

我的JavaScript是一个有点生疏,但这样的事情应该工作:

var models = []; 
$('#ctl00_body_modellistGrid a[href*="ModelDetail"]').each(function(i) { 
    // Note that we're using the optional index parameter of the each function, 
    // which I have called 'i' 
    // We will create an object with a label and a value and store it in models[i] 
    models[i] = { label: $(this).text(), value: $(this).attr("href") }; 
}); 

// We will now pass this array of objects to the autocomplete function 
// The autocomplete function, if given an array of objects, is expecting 
// two properties: 'label' and 'value' 
$("input#autocomplete").autocomplete({ 
    source: models, 
    // We will also provide a function when the user selects an option 
    select: function(event, ui) { 
     // ui.item should hold the object we passed in, I think 
     // Let's redirect the user to the url 
     // ui.item.value should hold the url 
     window.location.href = ui.item.value; 
    } 
}); 

我相信上面的应该工作。我把一个非常基本的演示上的jsfiddle: - >http://jsfiddle.net/B3dgW/

+0

呵呵我也挖了我以前的PHP项目中,我使用的自动完成,你应该提供正常工作的代码。如果autor不想更新数组创建部分,您仍然可以使用'ui.item.label'而不是'ui.item.value',因为我可以使用select保存url的 – arma 2011-04-03 01:49:40

+0

@arma - 谢谢!很高兴听到我不是生锈的。 ;-)你不必删除你的答案 - 它会一样正确。 – Pandincus 2011-04-03 01:51:18

+0

Na,你的答案更漂亮:D – arma 2011-04-03 01:53:27