0
我使用AutoSuggest作为搜索建议将项目添加到Grocerylist(groclist)。当用户选择一个建议时,它应该使用AJAX将该项目添加到GroceryList,并重新加载该购物清单上的项目。我使用Jquery AutoComplete in CakePHP发现的教程来完成自动完成功能,但我被卡住了。如何使用JQuery Autosuggest onItemSelect事件在CakePHP的HABTM表中添加条目?
如何添加关联?
$(document).ready(function(){
$("#autoComplete").autocomplete("/groceries/items/autoComplete",
{
minChars: 2,
cacheLength: 10,
onItemSelect: selectItem,
onFindValue: findValue,
formatItem: formatItem,
selectFirst: true
});
});
// This is was here
function selectItem(li) {
findValue(li);
}
function addAssociation(li) {
}
function findValue(li) {
if(li == null) {return alert("No match!");}
// if coming from an AJAX call, let's use the product id as the value
if(!!li.extra) {var sValue = li.extra[0];}
// otherwise, let's just display the value in the text box
else {var sValue = li.selectValue; }
//alert("The value you selected was: " + sValue);
}
function formatItem(row) {
if(row[1] == undefined) {
return row[0];
}
else {
//return row[0] + " (id: " + row[1] + ")"; original displays Cake (id: 1)
return row[0];
}
}
我有点不确定如何将这与我目前的JavaScript合并。 – Chris 2012-02-01 15:53:35
@Chris我更少更新答案,这就是你需要做的 – api55 2012-02-01 19:39:42