链接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
最满意答案
通常,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.
相关问答
更多-
SQL主键可以接受'0'?(SQL primary key can accept '0'?)[2024-01-11]
主键可以为零,但是如果在列上设置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.
-
SQL Server 2008优于SQL Server 2005?(Advantages of SQL Server 2008 over SQL Server 2005? [closed])[2022-06-16]
透明数据加密。 加密整个数据库的能力。 备份加密。 在备份时间执行,以防止篡改。 外部管理。 将密钥与数据分开存储。 审计。 监控数据访问。 数据压缩 事实表减小尺寸并提高性能。 资源总监。 限制用户或组消耗高级别或资源。 热插拔CPU。 新增CPU。 性能工作室 收集性能监控工具。 安装改进。 磁盘映像和服务包卸载选项。 动态发展。 新的ADO和Visual Studio选项以及Dot Net 3。 实体数据服务。 业务线(LOB)框架和实体查询语言(eSQL) LINQ。 用于访问SQL和XML等多种数 ... -
任何人都知道如何在数百万行的SQL Server 2008中高效地进行分页工作? 如果您想要精确的完美分页,则无法替代为每条记录构建索引键(位置行号)。 但是,还有其他选择。 (1)总页数(记录) 假设变化率很小,您可以使用sysindexes.rows (几乎即时)的近似值。 您可以使用触发器来保持一个完全准确的,到第二个表格行数 (2)寻呼 (一个) 您可以在接下来的五页中向页面的任一侧显示页面跳转。 这些需要在每边最多扫描{页面大小} x 5。 如果您的基础查询适合快速沿着排序顺序旅行,这应该不会太慢 ...
-
将python 3.3连接到microsoft sql server 2008(Connecting python 3.3 to microsoft sql server 2008)[2021-11-24]
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 ...