【发布时间】:2013-09-29 08:47:03
【问题描述】:
我有一个文本框,在点击或输入单词时,用户会看到一些建议,问题是建议是可见的,并且使用文本框内的向上/向下键导航到建议也是它的导航
下面是代码:
<label>when</label>
<div class="fl">
<input class="date-field startdate cornerall" type="text"/>
<div class="input-wrapper">
<input type="text" class="starttime cornerall" placeholder="Add a Time?"/>
<div class="dropdown stsugstn hidden">
<ul class="result cornerall">
<li class="sugactive">11.05</li>
<li>11.05</li>
<li>11.05</li>
<li>11.05</li>
<li>11.05</li>
</ul>
</div>
</div>
<a href="#" class="timezone title-tip" title="IST">GMT+5:30</a>
<a href="#" class="endtimeln">End Time?</a>
</div>
各自的jquery:
$('.cnewrap').on({
keyup: function(e){
var $this = $(this);
var $vl = $this.val();
var $st = $this.parent().find('.stsugstn');
var key = (e.keyCode ? e.keyCode : e.which);
if($vl != null && $vl.length > 0){
if($st.hasClass( "hidden" )){
$st.removeClass('hidden');
}
if (key === 38 || key === 40){
//TODO write code to stop navigation inside text box
//$this.val($vl);
}
}else{
if(!$st.hasClass( "hidden" )){
$st.addClass('hidden');
}
}
},
click: function(e){
var $this = $(this);
var $vl = $this.val();
var $st = $this.parent().find('.stsugstn');
if($vl != null && $vl.length > 0 && $st.hasClass( "hidden" )){
$st.removeClass('hidden');
}
}
},'.starttime');
jquery 用于在建议之间导航:
$(window).on('keyup', function(e){
var $stsugstn = $('.stsugstn');
var $etsugstn = $('.etsugstn');
var $next = '';
var key = (e.keyCode ? e.keyCode : e.which);
if($stsugstn!= null && $stsugstn.length > 0 && !$stsugstn.hasClass( "hidden" )){
var $current = $('.stsugstn ul li.sugactive');
if (key === 38){
if($current.is(':first-child')){
$next = $('.stsugstn ul li:last-child');
}else{
$next = $current.prev();
}
}
if (key === 40){
if($current.is(':last-child')){
$next = $('.stsugstn ul li:first-child');
}else{
$next = $current.next();
}
}
if ($next.length > 0) {
$current.removeClass('sugactive');
$next.addClass('sugactive');
}
}
if($etsugstn!= null && $etsugstn.length > 0 && !$etsugstn.hasClass( "hidden" )){
var $current = $('.etsugstn ul li.sugactive');
if (key === 38){
if($current.is(':first-child')){
$next = $('.etsugstn ul li:last-child');
}else{
$next = $current.prev();
}
}
if (key === 40){
if($current.is(':last-child')){
$next = $('.etsugstn ul li:first-child');
}else{
$next = $current.next();
}
}
if ($next.length > 0) {
$current.removeClass('sugactive');
$next.addClass('sugactive');
}
}
});
请建议我如何在使用向上/向下键在建议中导航时停止在文本框内导航 正如 jsfiddle 所建议的那样, 链接在这里 http://jsfiddle.net/Brg9t/
【问题讨论】:
-
小提琴会有所帮助,但是.. 好吧.. 当您检测到键向上/向下时,调用 e.preventDefault() 可能会起作用..
-
我尝试使用 e.preventDefault() 但它对我不起作用..而是我把 return false;这似乎有效,但它停止了建议之间的导航
-
so return false 类型的作品.. 你把 return false 放在哪里了?
-
@user1600124:在文本框查询内,即我标记为 //TODO
-
请放小提琴
标签: javascript jquery