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>