JQuery,ASP:Gridview和自动完成-需要自动完成值的链接

JQuery,ASP:Gridview和自动完成-需要自动完成值的链接,jquery,Jquery,我有一个创建Gridview的ASPX页面。我的JQuery函数从Gridview数据中提取值来填充我的JQuery自动完成源。这一切都很好。现在我想让我的自动完成值在单击时转到链接。该链接也在Gridview中,我使用text()获取文本值。(我在Gridview中搜索包含“ModelDetail”的链接,并将文本存储到数组中)我是否需要JQuery二维数组?如果是,我该怎么做?这是我的密码: $(文档).ready(函数(){ var modelnames=新数组(); $('#ctl0

我有一个创建Gridview的ASPX页面。我的JQuery函数从Gridview数据中提取值来填充我的JQuery自动完成源。这一切都很好。现在我想让我的自动完成值在单击时转到链接。该链接也在Gridview中,我使用text()获取文本值。(我在Gridview中搜索包含“ModelDetail”的链接,并将文本存储到数组中)我是否需要JQuery二维数组?如果是,我该怎么做?这是我的密码:


$(文档).ready(函数(){
var modelnames=新数组();
$('#ctl00_body_modellistGrid a[href*=“ModelDetail”]')。每个(函数(){
modelnames.push($(this.text());
})
$(“输入自动完成”)。自动完成({
资料来源:型号名称
});
});
提前谢谢!
Bob

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;
    }
});

我相信上述方法应该有效。我在JSFIDLE上做了一个非常基本的演示:-->

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;
    }
});


我相信上述方法应该有效。我在JSFIDLE上做了一个非常基本的演示:-->

您使用的是哪个自动完成插件?这个@Pandincus-看起来像jQueryUI@arma-哦,我现在明白了,原来的插件被弃用了,取而代之的是jQueryUI插件。谢谢我是jQueryUIAutoComplete1.8.11。对不起,我应该说的。你用的是哪个自动完成插件?这个@Pandincus-看起来像jQueryUI@arma-哦,我现在明白了,原来的插件被弃用了,取而代之的是jQueryUI插件。谢谢我是jQueryUIAutoComplete1.8.11。抱歉,我应该说。呵呵,我还挖掘了我以前的php项目,在那里我使用了autocomplete,您提供的代码应该可以正常工作。如果autor不想更新数组创建部分,您仍然可以使用
ui.item.label
而不是
ui.item.value
,因为当我获得select holds url's@arma-谢谢!很高兴听到我没那么生疏你不必删除你的答案——它也会一样正确。不,你的答案更漂亮:DApparently,你的JavaScript没有那么生锈。谢谢,效果很好。@Bob-在我回答问题时正在重新学习;-)很高兴听到这个消息——很高兴编码!呵呵,我还挖掘了我以前的php项目,在那里我使用了autocomplete,您提供的代码应该可以正常工作。如果autor不想更新数组创建部分,您仍然可以使用
ui.item.label
而不是
ui.item.value
,因为当我获得select holds url's@arma-谢谢!很高兴听到我没那么生疏你不必删除你的答案——它也会一样正确。不,你的答案更漂亮:DApparently,你的JavaScript没有那么生锈。谢谢,效果很好。@Bob-在我回答问题时正在重新学习;-)很高兴听到这个消息——很高兴编码!