2012-07-13 43 views
0

我有一个包含项目的列表。
当我点击这些项目中的任何一个时,我将复制将其id-值转换成text -field的形式。
每次我点击它,它会取代该值,默认情况下这是正确的。但是我想补充一点,用户可以在键盘上按住一个按键,然后点击它们,只需点击.append即可,无论他们只是点击了哪一个表单字段。单击以替换值,shift +单击以使用jquery附加值

这里是我的jQuery的代码,我使用的第一/默认方案:

$(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 
    }); 
}); 

是否有实现某种形式的键盘键侦听的方式吗?
喜欢的东西:

if (e.shiftKey){ 
    .append('this text instead') 
} 

还没有尝试过,看看是否shiftKey是即使在这里

回答

2

shiftKey是事件对象的属性之一,并且可以使用。试试这个:

$(document).on('keyup click', function(e){ 
    if (e.shiftKey) { 
     $('input#search').focus() 
     $('input#search').val(e.target.id) 
    } 
}) 

​​

1

任何有效的名称有是一个jQuery插件,延长点击。

你可以尝试一下,看看他们是如何做到的,并自己实现它。

ExtendedClick plugin

希望这有助于。

2
$('ul#filter-results').on('click', 'li', function(e) { 
    if(e.shiftKey) { 
    do something; 
    } else { 
    do something else; 
    } 
}); 
0

这是我结束了:
我切换到altKey因为shiftKey标志着大量的文字时,我点击。
没有做任何事情,除了它不好看...

$(function(){ 
    $('ul#filter-results li').click(function(e){ 

     var text = $(this).attr('id'); // get the ID 
     var input = $('#search'); // form field to insert text into 

     if (e.altKey){ input.val(input.val()+', '+text+' ').keyup(); } // fetch whatever is already there, and add some more 
     else { input.val(text+' ').keyup(); } // just replace whatever is already there 

     $('#search').focus(); 

    }); 
}); 

感谢好的建议?