zoukankan      html  css  js  c++  java
  • jquery 回车事件

    应用场景是这样的:有一个搜索框,输入关键字执行搜索.

    所以写下以下代码

    $("#txt").keydown(function (event) {
    if (event.keyCode == 13) {
    $("#btn").trigger("click");
    }
    });

    但是总是被刷新掉,但是找了半天没有找到刷新的原因.

    最后还是度娘啊~~度娘啊~~~,终于找到原因

    <form id="form1" runat="server">
    
    <input id="txtName" type="text" class="form-control" placeholder="请输入关键字" aria-describedby="basic-addon2"/>
    
    </form>

    如果表单form中只包含一个input 控件的时候,回车会自动提交,引起页面刷新事件,导致 查找失败.

    解决方法有两种,

    一  是再加入一个 input 控件就可以了.只要不显示此控件就可以了.这样在输入文字之后,再按回车就不会自动提交刷新了.

     <form id="form1" runat="server"> 
                           <input id="txtName" type="text" class="form-control" placeholder="请输入终端名称" aria-describedby="basic-addon2">
                            <input style="display: none;" type="text" /> 
    </form>

    二 添加onkeydown事件

    <form id="form1" runat="server">
    
    <input id="txtName" type="text" class="form-control" placeholder="请输入关键字" aria-describedby="basic-addon2"  onkeydown='if(event.keyCode==13){gosubmit();}' />
    
    </form>

    总结:

    如果表单里有一个type=”submit”的按钮,回车键生效。  
    如果表单里只有一个type=”text”的input,不管按钮是什么type,回车键生效。  
    如果按钮不是用input,而是用button,并且没有加type,IE下默认为type=button,FX默认为type=submit。  
    其他表单元素如textarea、select不影响,radio checkbox不影响触发规则,但本身在FX下会响应回车键,在IE下不响应。  
    type=”image”的input,效果等同于type=”submit”,不知道为什么会设计这样一种type,不推荐使用,应该用CSS添加背景图合适些。 

    来自:http://www.cnblogs.com/luoyanli/archive/2012/07/09/2582650.html

  • 相关阅读:
    poj 1113 Wall 凸包的应用
    NYOJ 78 圈水池 (入门级凸包)
    Monotone Chain Convex Hull(单调链凸包)
    poj Sudoku(数独) DFS
    poj 3009 Curling 2.0(dfs)
    poj 3083 Children of the Candy Corn
    Python join()方法
    通过FISH和下一代测序检测肺腺癌ALK基因融合比较
    华大病原微生物检测
    NGS检测ALK融合大起底--转载
  • 原文地址:https://www.cnblogs.com/BinBinGo/p/4926287.html
Copyright © 2011-2022 走看看