单击以替换值,Shift + 单击以使用 jquery 附加值 [英] click to replace value, shift + click to append value using jquery
问题描述
我有一个物品清单.
当我点击这些项目中的任何一个时,我复制它的 id
-value 到一个表单 text
-field 中.
每次单击时,它都会替换该值,默认情况下这是正确的.但我想补充的是,用户可以按住键盘上的一个键,然后当他们单击时,他们只需 .append
任何他们刚刚在同一个表单字段中单击的内容.
I have a list with items.
When I click any of these items, I copy its id
-value into a form text
-field.
Everytime I click, it replaces the value, which is correct by default. But what I would like to add, is a way for the user to hold down a key on their keyboard, and when they then click, they just .append
whatever they just clicked into the same form field.
这是我用于第一个/默认场景的 jQuery 代码:
Here's my jQuery-code I'm using for the first/default scenario:
$(function(){
$('ul#filter-results li').click(function(){
var from = $(this).attr('id'); // get the list ID and
$('input#search').val(from+' ').keyup(); // insert into text-field then trigger the search and
$('input#search').focus(); // make sure the field is focused so the user can start typing immediately
});
});
有没有办法实现某种键盘按键监听器?
类似的东西:
Is there a way to implement some sort of keyboard key-listener?
Something like:
if (e.shiftKey){
.append('this text instead')
}
还没有试过在这里查看 shiftKey
是否是任何有效名称
haven't tried out to see if shiftKey
is even any valid name here
推荐答案
shiftKey
是事件对象的属性之一,可以使用.试试这个:
shiftKey
is of one of the properties of the event object and is valid to be used. try this:
$(document).on('keyup click', function(e){
if (e.shiftKey) {
$('input#search').focus()
$('input#search').val(e.target.id)
}
})
这篇关于单击以替换值,Shift + 单击以使用 jquery 附加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!