tp5頁面輸出時,搜尋功能在跳到下一頁時,如果不做任何處理,會傳回原有是第二頁輸出的資料。為了確保跳轉下一頁時輸出的是搜尋到的數據,做以下處理。
(要根據自己的搜尋欄位進行適當修改)
頁面js程式碼,為網址列加上參數,以便於點選搜尋按鈕後台透過網址列得到搜尋條件。
$(document).ready(<span style="color: #0000ff;">function</span><span style="color: #000000;">(){<br> <span style="color: #008000;">//点击搜索按钮执行搜索</span> $(</span>"<span style="color: #0000ff;">#search</span>").click(<span style="color: #0000ff;">function</span><span style="color: #000000;">() { </span><span style="color: #0000ff;">var</span> form = $('#form'<span style="color: #000000;">); </span><span style="color: #0000ff;">var</span> url = form.attr('action'<span style="color: #000000;">); </span><span style="color: #0000ff;">var</span> query = $('.key'<span style="color: #000000;">).serialize(); query </span>= query.replace(/(&|^)(\w*?\d*?\-*?_*?)*?=?((?=&)|(?=$))/g, ''<span style="color: #000000;">); query </span>= query.replace(/^&/g, ''<span style="color: #000000;">); </span><span style="color: #0000ff;">if</span> (url.indexOf('?') > 0<span style="color: #000000;">) { url </span>+= '&' +<span style="color: #000000;"> query; } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { url </span>+= '?' +<span style="color: #000000;"> query; } form.attr(</span>'action'<span style="color: #000000;">,url); }); </span><span style="color: #008000;">//回车搜索</span> $(".search-form").keyup(<span style="color: #0000ff;">function</span><span style="color: #000000;">(e) { </span><span style="color: #0000ff;">if</span> (e.keyCode === 13<span style="color: #000000;">) { $(</span>".search"<span style="color: #000000;">).click(); </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">; } });<br> <span style="background-color: #ffffff; color: #008000;">//设定input框的值,保留搜索条件,setValue的方法参照http://www.cnblogs.com/xiaopiyuanzi/p/5920864.html</span> setValue(</span>'shop_id', "{:input('get.shop_id')}"<span style="color: #000000;">); setValue(</span>'key', "{:input('get.key')}"<span style="color: #000000;">); }) </span>
搜尋部分頁面html代碼
<span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="search search-form"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="background: #e0e0e0;margin-bottom: 10px"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">form </span><span style="color: #ff0000;">action</span><span style="color: #0000ff;">="{:url('news')}"</span><span style="color: #ff0000;"> id</span><span style="color: #0000ff;">="form"</span><span style="color: #ff0000;"> method</span><span style="color: #0000ff;">="post"</span><span style="color: #ff0000;"> <table border</span><span style="color: #0000ff;">="0"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="height: 50px;font-size: 15px;color: black;"</span><span style="color: #ff0000;"> cellpadding</span><span style="color: #0000ff;">="5"</span><span style="color: #ff0000;"> width</span><span style="color: #0000ff;">="35%"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">tr</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">td</span><span style="color: #0000ff;">></span><span style="color: #000000;">商品类: </span><span style="color: #0000ff;"><</span><span style="color: #800000;">select </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="shop_id"</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="key"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">option </span><span style="color: #ff0000;">value</span><span style="color: #0000ff;">="all"</span><span style="color: #0000ff;">></span>全部<span style="color: #0000ff;"></</span><span style="color: #800000;">option</span><span style="color: #0000ff;">></span><span style="color: #000000;"> {volist name='li' id='k'} </span><span style="color: #0000ff;"><</span><span style="color: #800000;">option </span><span style="color: #ff0000;">value</span><span style="color: #0000ff;">="{$k.id}"</span><span style="color: #0000ff;">></span>{$k.name}<span style="color: #0000ff;"></</span><span style="color: #800000;">option</span><span style="color: #0000ff;">></span><span style="color: #000000;"> {/volist} </span><span style="color: #0000ff;"></</span><span style="color: #800000;">select</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">td</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">td</span><span style="color: #0000ff;">></span>标题:<span style="color: #0000ff;"><</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="key"</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="key"</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="inpMain"</span><span style="color: #ff0000;"> placeholder</span><span style="color: #0000ff;">="新闻标题或标题包含字"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">td</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">td</span><span style="color: #0000ff;">><</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="submit" <span style="color: #ff0000;">id</span>="search"</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="btn"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="搜索"</span> <span style="color: #0000ff;">/></</span><span style="color: #800000;">td</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">tr</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">table</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">form</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
控制器主要程式碼
<span style="color: #0000ff;">if </span>(empty(input('get.'<span style="color: #000000;">))) { $page </span>= NewsModel::where('isdelete',1)->order('id desc')->paginate(8<span style="color: #000000;">); $</span><span style="color: #0000ff;">this</span>->assign('list'<span style="color: #000000;">,$page); </span><span style="color: #0000ff;"> return</span> $<span style="color: #0000ff;">this</span>-><span style="color: #000000;">fetch(); } </span><span style="color: #0000ff;">else </span><span style="color: #000000;">{ $shop_id</span>=input('get.shop_id'<span style="color: #000000;">); $key</span>=input('get.key'<span style="color: #000000;">); </span><span style="color: #0000ff;"> if </span>($shop_id=='all' && $key != ''<span style="color: #000000;">) { $page</span>=NewsModel::where(['title'=>['like',"%$key%"],'isdelete'=>['eq',1]])->order('id desc')->paginate(8,<span style="color: #0000ff;">false</span><span style="color: #000000;">,[ </span> 'query' => ['shop_id' => $shop_id,'key' => $key] <span style="color: #008000;">//</span><span style="color: #008000;">url额外参数</span> <span style="color: #000000;"> ]); } elseif ($shop_id </span>!= 'all' && $key != ''<span style="color: #000000;">) { $page</span>=NewsModel::where(['title'=>['like',"%$key%"],'shop_id'=>['eq',$shop_id],'isdelete'=>['eq',1]])->order('id desc')->paginate(8,<span style="color: #0000ff;">false</span><span style="color: #000000;">,[ </span> 'query' => ['shop_id' => $shop_id,'key' =><span style="color: #000000;"> $key] ]); } elseif ($shop_id </span>!= 'all' && $key == ''<span style="color: #000000;">) { $page</span>=NewsModel::where(['shop_id'=>['eq',$shop_id],'isdelete'=>['eq',1]])->order('id desc')->paginate(8,<span style="color: #0000ff;">false</span><span style="color: #000000;">,[ </span> 'query' => ['shop_id' => $shop_id,'key' =><span style="color: #000000;"> $key] ]); } </span><span style="color: #0000ff;">else </span><span style="color: #000000;">{ $page</span>=NewsModel::where('isdelete',1)->order('id desc')->paginate(8,<span style="color: #0000ff;">false</span><span style="color: #000000;">,[ </span> 'query' => ['shop_id' => $shop_id,'key' =><span style="color: #000000;"> $key] ]); } $</span><span style="color: #0000ff;">this</span>->assign('list'<span style="color: #000000;">,$page); </span><span style="color: #0000ff;"> return</span> $<span style="color: #0000ff;">this</span>-><span style="color: #000000;">fetch(); }</span>