weixin_33705053 2012-12-15 10:05 采纳率: 0%
浏览 18

防止搜索时刷新Ajax

I have this code which permits me to reload the content of a page every few seconds.

function()
{
$('#lista').show().load('listaupdate.php').fadeIn("slow");}, 3000);

Now, the issue is, i have a search form there, but, how should i prevent refreshing when searching?

Or it can refresh ok, but after the search has been done, and after the results were given. It's the same issue with pagination, if i go to page 2, then it refreshes and takes me to page 1.

How can i solve this? Thanks

search form:

<form action="listasearch.php" method="post">
<select name="kategoria">
  <option value="dyqani_pergjegjes">Dyqani Përgjegjës</option>    
</select> 
<input type="text" name="search">
 <input type="submit">
</form>
  • 写回答

1条回答 默认 最新

  • YaoRaoLov 2012-12-15 10:35
    关注

    The way I see this you have two options:

    1. Option 1 : Make the listaupdate.php return just the data and make AJAX call to it, rather than load and the build the markup in the complete handler (the idea is there it no point to reload the whole page, including the search form - just the data that needs updating). See http://api.jquery.com/jQuery.get/

    2. Option 2 - cancel updates when search field is being used by the user ( frankly not the better solution, but still). Do something like :

      var interval = setInterval(function(){
      $('#lista').show().load('listaupdate.php').fadeIn("slow");}, 3000);
      

    listen for some search-related event (make one of your own if focus deosn't fit the case):

    $('input[name="search"]').on("focus", function(event){
         interval = clearInterval(interval);
    });
    
    // restart it when search is done
    $("form").submit( function(event){
             interval = setInterval(function(){
            $('#lista').show().load('listaupdate.php').fadeIn("slow");}, 3000);
      });
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!