2012-08-09 79 views
0

我的自动完成数据以这样的形式出现{“label”:“Apple iPhone 3G”,“values”:“293”} ..等。 当用户点击自动填充选项i想要将它们重定向到基于唯一ID值的产品页面。例如,当iphone 3g被挑选出来的时候,我想让它去example.com/293 ,但是目前它需要输入什么,而不是选择什么。 所以说我输入A并点击iphone 4g,它将不会存储iphone 4g, 的正确值,而是会存储以a开头的第一个项目的值。或者如果我在iphone和选择iPhone 4型,这将节省的第一代iPhone的价值在数据库如何正确存储来自自动完成的值

http://mogulcases.com/matt/search.php

如何IM存储键值

$.widget("custom.catcomplete", $.ui.autocomplete, { 
    _renderMenu: function(ul, items) { 
    var self = this, 
       currentCategory = ""; 

    $.each(items, function(index, item) { 
     self._renderItem(ul, item); 

     $("input[type=hidden][name=key]").val(item.values); 

    }); 

    } 
}); 

IM如何抓住值

var searchcode = document.getElementById('key').value; 

回答

0

为什么不只是用select事件做些什么。即

$(".selector").autocomplete({ 
    select: function(event, ui) { 
    window.location = "http://example.com/" + ui.item.id //would send them to the page you want them to go if they click on this 
    } 
}); 

这样,如果用户从列表中选择一些东西,你可以从那里调用事件并知道该项目存在。如果他们没有从列表中选择它,它将在他们点击搜索按钮或按下回车键时执行搜索。

你甚至可以更改该值上选择同样的方式

$(".selector").autocomplete({ 
    select: function(event, ui) { 
    $(".selector").val(ui.item.value); //I think Autocomplete does something like this automatically so may not be needed. 
    //But you could do something like this 
    $("input[type=hidden][name=key1]").val(ui.item.value); 
    } 
}); 

文档可以发现here

+0

非常感谢队友,救了我一个巨大的头痛! – matture 2012-08-10 13:56:40

+0

没问题。我在编程时所学到的几乎所有东西都是,如果它以单向方式导致太多令人头疼的事情,通常还有另一种方式。 – 2012-08-10 14:39:45