【发布时间】:2014-10-01 09:28:36
【问题描述】:
我有点卡在一段代码上。我有一个搜索字段,它调用一个查看数据库的 PHP 函数:
$this->_helper->layout->disableLayout();
$q = $this->getRequest()->getParam('q');
$product_db = new Products();
$this->view->products = $product_db->searchProduct($q);
foreach($this->view->products as $product)
{
....
}
结果通过 JQuery 加载到我的 HTML 页面中:
var searchItem = function() {
if($('#json_search').val())
{
$('#search-resultlist').load('/search/quick/q/'+ $('#json_search').val() );
}
else {
$('#search-resultlist').html('');
}
}
HTML:
<div class="right-searchcontent" >
<input type="text" class="form-control" placeholder="Search ... " id="json_search">
</div>
<div class="right-searchresult">
<ul id="search-resultlist" >
<li >
</li>
</ul>
</div>
现在基本上我试图实现的是创建一个“搜索历史”。 起初,我在搜索控制器中使用 SESSION 数组进行了尝试:
if(!isset($_SESSION['history'])) {
$_SESSION['history'] = array();
}
并在我的函数中显示数据库搜索结果:
if(!empty($product)){
if(!in_array($product->naam, $_SESSION['history']))
{
$_SESSION['history'][] = $product->naam;
}
}
但这存储了我曾经搜索过的所有值(例如:'sk'、'ah') 我只想要我实际点击的值。
谁能指出我正确的方向?
我一直在尝试使用 localStorage 来实现我的结果,但这不会为我提供正确的解决方案。我还尝试使用带有此功能的 Cookie:
var cookieList = function(cookieName) {
var cookie = $.cookie(cookieName);
var items = cookie ? cookie.split(/,/) : new Array();
return {
"add": function(val) {
//Add to the items.
items.push(val);
//Save the items to a cookie.
$.cookie(cookieName, items.join(','));
},
"remove": function (val) {
indx = items.indexOf(val);
if(indx!=-1) items.splice(indx, 1);
$.cookie(cookieName, items.join(',')); },
"clear": function() {
items = null;
//clear the cookie.
$.cookie(cookieName, null);
},
"items": function() {
return items;
}
}
}
但是当我尝试提醒 list.items 它只是返回给我整个方法。
当我点击它时,我可以实现产品名称,但我只是不知道如何将它存储到 SESSION 或其他我可以在任何时间在任何页面上实现的东西中......
$(document).on('click', 'a.searchItem', function(e){
e.preventDefault();
var search = $(this).attr('value'));
});
【问题讨论】:
-
你可以使用jQuery和cookies来存储被点击的cookies
-
今天早上我一直在玩 Cookies,但我无法正确使用它。我编辑了我的问题并添加了我用于 cookie 的函数。
-
alert(list.items())而不是alert(list.items) -
哦哈哈,我真是个愚蠢的错误!感谢您指出这一点
标签: javascript php jquery html zend-framework