首页 \ 问答 \ 链接jquery keydown事件(Chaining jquery keydown event)

链接jquery keydown事件(Chaining jquery keydown event)

我试图创建一个下拉式功能,一旦有人开始在输入框中输入,就会显示div。 我能够在单个元素上附加鼠标单击事件以选择它们。 但我无法通过按Enter键来选择它们。

HTML

<input type="text" class="input" name="example" id="example" placeholder="example..." autocomplete="off" list="examplelist" />

<div class="autofill">
    <ul class="autocomplete">
        <li class="autocomplete-list">John Doe (San Jose, CA)</li>
        <li class="autocomplete-list">Jane Doe (San Francisco, CA)</li>
        <li class="autocomplete-list">John Jane (San Carlos, CA)</li>
    </ul>
</div>

JQuery的

$(document).ready(function () {
var $listItems = $('li.autocomplete-list'),
    $div = $('div.autofill'),
    $input = $('#example');

$div.hide();

$('input#example').on('keydown', function (e) {
    var key = e.keyCode,
        $selected = $listItems.filter('.selected'),
        $current;

    $div.show();

    if (key != 40 && key != 38) return;

    $listItems.removeClass('selected');

    if (key == 40) { // Down key
        if (!$selected.length || $selected.is(':last-child')) {
            $current = $listItems.eq(0);
        } else {
            $current = $selected.next();
        }
    } else if (key == 38) { // Up key
        if (!$selected.length || $selected.is(':first-child')) {
            $current = $listItems.last();
        } else {
            $current = $selected.prev();
        }
    }

    $current.addClass('selected');

    // When I press enter after selecting the li element
    // Does not work :(
    $current.on('keypress keydown keyup', 'li', function (event) {
        if (event.keyCode == 13) {
            var value = $(this).text().split('(')[0].trim();
            $input.val(value) ;
            $div.hide();
        }
    });
});


// If somebody clicks on the li item
$('li.autocomplete-list').on('click', function (e) {
    var value = $(this).text().split('(')[0].trim();
    $input.val(value);
    $div.hide();
});

// change color on hover
$('li.autocomplete-list').hover(
    function(){ $(this).addClass('hover') },
    function(){ $(this).removeClass('hover') }
);

// When I press enter after selecting the li element
// Does not work :( 

$('li.autocomplete-list').on('keypress keydown keyup', 'li', function (event) {
    if (event.keyCode == 13) {
        var value = $(this).text().split('(')[0].trim();
        $input.val(value) ;
        $div.hide();
    }
  });
});

如何选择特定的li元素,然后在按下输入时取其值? 这是JSFiddle


I am trying to create a drop down like functionality where a div is shown once somebody starts typing in the input box. I am able to attach mouse click event on the individual elements to select them. But I am not able to select them via pressing enter.

HTML

<input type="text" class="input" name="example" id="example" placeholder="example..." autocomplete="off" list="examplelist" />

<div class="autofill">
    <ul class="autocomplete">
        <li class="autocomplete-list">John Doe (San Jose, CA)</li>
        <li class="autocomplete-list">Jane Doe (San Francisco, CA)</li>
        <li class="autocomplete-list">John Jane (San Carlos, CA)</li>
    </ul>
</div>

JQuery

$(document).ready(function () {
var $listItems = $('li.autocomplete-list'),
    $div = $('div.autofill'),
    $input = $('#example');

$div.hide();

$('input#example').on('keydown', function (e) {
    var key = e.keyCode,
        $selected = $listItems.filter('.selected'),
        $current;

    $div.show();

    if (key != 40 && key != 38) return;

    $listItems.removeClass('selected');

    if (key == 40) { // Down key
        if (!$selected.length || $selected.is(':last-child')) {
            $current = $listItems.eq(0);
        } else {
            $current = $selected.next();
        }
    } else if (key == 38) { // Up key
        if (!$selected.length || $selected.is(':first-child')) {
            $current = $listItems.last();
        } else {
            $current = $selected.prev();
        }
    }

    $current.addClass('selected');

    // When I press enter after selecting the li element
    // Does not work :(
    $current.on('keypress keydown keyup', 'li', function (event) {
        if (event.keyCode == 13) {
            var value = $(this).text().split('(')[0].trim();
            $input.val(value) ;
            $div.hide();
        }
    });
});


// If somebody clicks on the li item
$('li.autocomplete-list').on('click', function (e) {
    var value = $(this).text().split('(')[0].trim();
    $input.val(value);
    $div.hide();
});

// change color on hover
$('li.autocomplete-list').hover(
    function(){ $(this).addClass('hover') },
    function(){ $(this).removeClass('hover') }
);

// When I press enter after selecting the li element
// Does not work :( 

$('li.autocomplete-list').on('keypress keydown keyup', 'li', function (event) {
    if (event.keyCode == 13) {
        var value = $(this).text().split('(')[0].trim();
        $input.val(value) ;
        $div.hide();
    }
  });
});

How can I select a particular li element and then take its value when press enter? Here is the JSFiddle


原文:https://stackoverflow.com/questions/30065428
更新时间:2023-07-20 08:07

最满意答案

通常,KEY是唯一标识表中每一行的列(或列的组合)。 表中可能有多个KEY(例如,您可能有一个Person表,其中社会安全号码和自动递增号码都是关键字)。

数据库设计人员选择其中一个 KEY作为PRIMARY KEY。 从概念上讲,选择哪个 KEY作为PRIMARY KEY并不重要。 但是,由于PRIMARY KEY通常用于从其他表(通过FOREIGN KEY)引用此表中的条目,所以选择一个好的PRIMARY KEY可能与(a) 性能和(b) 可维护性有关

(a)由于主键通常用于JOIN中,因此主键上的索引(其大小,分布等)与其他索引的性能关系更密切。

(b)由于主键在其他表中用作外键,因此更改主键值总是麻烦,因为其他表中的所有外键值也需要修改。


In general, a KEY is a column (or combination of columns) that uniquely identifies each row in the table. It is possible to have multiple KEYs in a table (for example, you might have a Person table where both the social security number as well as an auto-increasing number are both KEYs).

The database designer chooses one of theses KEYs to be the PRIMARY KEY. Conceptually, it does not matter which KEY is chosen as the PRIMARY KEY. However, since the PRIMARY KEY is usually used to refer to entries in this table from other tables (through FOREIGN KEYs), choosing a good PRIMARY KEY can be relevant w.r.t. (a) performance and (b) maintainability:

(a) Since the primary key will usually be used in JOINs, the index on the primary key (its size, its distribution, ...) is much more relevant to performance than other indexes.

(b) Since the primary key is used as a foreign key in other tables, changing the primary key value is always a hassle, since all the foreign key values in the other tables need to be modified as well.

相关问答

更多
  • 主键可以为零,但是如果在列上设置Identity,它通常将从1开始而不是从0开始。 Primary Key Can be Zero, but if you set Identity on the column it normally will start at 1 rather than Zero.
  • 不,没有区别。 优化器以同样的方式对待它。 某些圈子甚至有争论主键本身是否真的有必要。 (尽管没有人会争辩说你应该为每个表至少定义一个唯一索引/约束)。 反对“主键”概念的争论可能会是这样的:如果你在一张桌子上有两列,并且它们都是唯一且数量相同的,那么这就成为PK了? 确实很好的问题。 无论如何,我总是使用PRIMARY KEY概念,因为从文档的角度来看它非常有帮助,它确实可以帮助人们理解你的意图。 No. There's really no difference. The optimizer treats ...
  • 使用“编辑顶部200”选项,然后单击“显示SQL面板”,使用WHERE子句修改查询,然后执行查询。 您将能够编辑结果。 Use the "Edit top 200" option, then click on "Show SQL panel", modify your query with your WHERE clause, and execute the query. You'll be able to edit the results.
  • 透明数据加密。 加密整个数据库的能力。 备份加密。 在备份时间执行,以防止篡改。 外部管理。 将密钥与数据分开存储。 审计。 监控数据访问。 数据压缩 事实表减小尺寸并提高性能。 资源总监。 限制用户或组消耗高级别或资源。 热插拔CPU。 新增CPU。 性能工作室 收集性能监控工具。 安装改进。 磁盘映像和服务包卸载选项。 动态发展。 新的ADO和Visual Studio选项以及Dot Net 3。 实体数据服务。 业务线(LOB)框架和实体查询语言(eSQL) LINQ。 用于访问SQL和XML等多种数 ...
  • 任何人都知道如何在数百万行的SQL Server 2008中高效地进行分页工作? 如果您想要精确的完美分页,则无法替代为每条记录构建索引键(位置行号)。 但是,还有其他选择。 (1)总页数(记录) 假设变化率很小,您可以使用sysindexes.rows (几乎即时)的近似值。 您可以使用触发器来保持一个完全准确的,到第二个表格行数 (2)寻呼 (一个) 您可以在接下来的五页中向页面的任一侧显示页面跳转。 这些需要在每边最多扫描{页面大小} x 5。 如果您的基础查询适合快速沿着排序顺序旅行,这应该不会太慢 ...
  • pyodbc支持python3并且可以连接到任何数据库,因为有一个odbc驱动程序,包括sql server。 还有一个纯python实现pypyodbc也应该支持python3。 adodbapi还声称可以使用python3。 在这里,您可以找到包含更多选项的列表。 pyodbc supports python3 and can connect to any databas for wich there's an odbc driver, including sql server. There's als ...
  • 通常,KEY是唯一标识表中每一行的列(或列的组合)。 表中可能有多个KEY(例如,您可能有一个Person表,其中社会安全号码和自动递增号码都是关键字)。 数据库设计人员选择其中一个 KEY作为PRIMARY KEY。 从概念上讲,选择哪个 KEY作为PRIMARY KEY并不重要。 但是,由于PRIMARY KEY通常用于从其他表(通过FOREIGN KEY)引用此表中的条目,所以选择一个好的PRIMARY KEY可能与(a) 性能和(b) 可维护性有关 : (a)由于主键通常用于JOIN中,因此主键上的 ...
  • 尝试这个: ALTER TABLE myTable ADD id INT IDENTITY CONSTRAINT id _pk PRIMARY KEY; Try this: ALTER TABLE myTable ADD id INT IDENTITY CONSTRAINT id _pk PRIMARY KEY;
  • 我相信我使用的密钥只检测工具是否已安装。 我将使用此密钥: SOFTWARE\Microsoft\Microsoft SQL Server\100\Bootstrap Release\1033\CurrentVersion\Version I believe the key I was using only detects if the tools are installed. I'm going to use this key: SOFTWARE\Microsoft\Microsoft SQL Serve ...
  • Windows 8 / Windows Server 2012支持SQL Server 2008,但需要最少的Service Pack 3。 选择Run this program (忽略警告)”,然后安装SQL Server 2008 SP3 http://www.microsoft.com/en-us/download/details.aspx?id=27594 。 请参阅http://blogs.msdn.com/b/psssql/archive/2012/09/01/installing-sql-se ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)