Rumah  >  Artikel  >  php教程  >  tp5页面输出时,搜索后跳转下一页的处理

tp5页面输出时,搜索后跳转下一页的处理

WBOY
WBOYasal
2016-10-08 16:04:412147semak imbas

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 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 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 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 style="color: #800000;">tr</span><span style="color: #0000ff;">></span>
                <span style="color: #0000ff;"><span style="color: #800000;">td</span><span style="color: #0000ff;">></span><span style="color: #000000;">商品类:
                    </span><span style="color: #0000ff;"><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 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 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 style="color: #800000;">td</span><span style="color: #0000ff;">></span>标题:<span style="color: #0000ff;"><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 style="color: #800000;">td</span><span style="color: #0000ff;">><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>                            

<p>控制器主要代码</p>
<div class="cnblogs_code">
<pre class="brush:php;toolbar:false"><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>

 

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:极验验证码安装部署教程Artikel seterusnya:PHP获取APK的包信息

Artikel berkaitan

Lihat lagi