bootstrap input框回车后重新刷新页面问题

简介: 需求是给bootstrap页面form表单中的input搜索框绑定回车事件,输入完成回车自动搜索

问题描述

在给bootstrap页面form表单中的input搜索框绑定回车事件后,输入完成点击回车搜索,页面会向后台发起两次请求,且会自动取消第一次请求,自动刷新页面导致不是你输入搜索条件查询到的结果,效果图如下

image.png

处理方案

处理方案是在input搜索框回车事件业务逻辑中,主动触发搜索事件之后返回false,让form表单不再进行列表刷新

页面代码如下

<form id="user-form">
  <input type="hidden" id="title" name="title" value="/">
  <input type="hidden" id="platform" name="platform" th:value="${platform}">
  <input type="hidden" id="filterFlag" name="filterFlag">
  <div class="select-list">
    <ul>
      <li>
        名称:<input type="text" name="fileName" id="fileName"/>
      </li>
      <li>
        <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()" id="search"><i class="fa fa-search"></i>&nbsp;搜索</a>
        <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
      </li>
      <li style="float: right">
          <button style="width: 40px;" class="btn btn-default btn-outline" type="button" onclick="$.table.search()" name="refresh" aria-label="刷新" title="刷新"><i class="glyphicon glyphicon-refresh icon-refresh"></i> </button>
          <button style="width: 40px;" class="btn btn-default btn-outline" type="button" onclick="toggleCustomView()" name="customView" aria-label="Toggle custom view" title="Toggle custom view"><i class="glyphicon glyphicon glyphicon-eye-open"></i> </button>
      </li>
    </ul>
  </div>
</form>

js代码如下

//回车事件
$("#fileName").bind("keydown",function(e){
  // 兼容FF和IE和Opera
  var theEvent = e || window.event;
  var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
  //console.log(code);
  if (code == 13) {
    //回车执行查询
    $("#search").trigger("click");
    return false;
  }
});

其中在回车执行查询之后的 return false;不能缺少,缺少就会出现上述问题,加上之后效果正常,如下图

image.png

相关文章
|
前端开发 JavaScript
Bootstrap File Input,最好用的文件上传组件(1)
Bootstrap File Input,最好用的文件上传组件
567 0
Bootstrap File Input,最好用的文件上传组件(1)
|
24天前
|
前端开发
BootStrap让两个控件在一行显示(label和input同行)
BootStrap让两个控件在一行显示(label和input同行)
111 0
|
11月前
|
Python
【Flask】flask-bootstrap报错AttributeError: module ‘dominate.tags‘ has no attribute ‘input‘解决方法
【Flask】flask-bootstrap报错AttributeError: module ‘dominate.tags‘ has no attribute ‘input‘解决方法
|
前端开发 程序员 内存技术
Bootstrap File Input,最好用的文件上传组件(2)
Bootstrap File Input,最好用的文件上传组件
249 0
Bootstrap File Input,最好用的文件上传组件(2)
|
前端开发
Bootstrap系列 -- 14. 表单控件输入框input
     每一个表单都是由表单控件组成。离开了控件,表单就失去了意义。接下来的我们简单的来了解Bootstrap框架中表单控件的相关知识。   单行输入框,常见的文本输入框,也就是input的type属性值为text。
1021 0
N..
|
24天前
|
开发框架 前端开发 UED
Bootstrap的CSS组件
Bootstrap的CSS组件
N..
16 0