简体   繁体   English

jQuery的自动填充/自动完成没有下拉

[英]jquery Autofill/Autocomplete without dropdown

I am trying to implement an 'autofill' ability to an input element. 我正在尝试对输入元素实施“自动填充”功能。

Basically, it accepts 4-digit years as input, from a range (2000 - 20012). 基本上,它接受范围为(2000-20012)的4位数的年份作为输入。 I'd like to know whether it's possible to have the input autofill itself (similar to Chrome's URL pane when typing something that you have previously typed) when the user types the first digit of the year. 我想知道当用户输入年份的第一位数字时,是否可以自动填充输入(类似于Chrome的URL窗格,当您键入以前输入的内容时)。

For example, when the user types "2", the input should autofill with, say, 2000. Then, the user could use the 'up' and 'down' arrows to cycle through the years. 例如,当用户键入“ 2”时,输入应自动填充为2000。然后,用户可以使用“向上”和“向下”箭头来循环显示年份。 The autofill should not be based on previous searches, just work in a fashion described above. 自动填充功能不应基于之前的搜索,而应以上述方式工作。 Also, it should not have a dropdown. 此外,它不应具有下拉菜单。

I've searched and searched but only found autocomplete with dropdowns, but no autofill. 我进行了搜索,但只发现带有下拉菜单的自动完成功能,但没有自动填充功能。 I also tried the <input type="number" /> for the up and down cycling part, but it does not have autofill and it does not work in Firefox. 我还尝试了上下循环部分的<input type="number" /> ,但是它没有自动填充功能,并且在Firefox中不起作用。

Is there a jQuery solution for this? 是否有jQuery解决方案?

LATER EDIT: 之后编辑:

I found the dateEntry plugin, yet I can't find a way to make it display the year as 4 digits. 我找到了dateEntry插件,但找不到使年份显示为4位数字的方法。 Tried various date formats such as 'Y' , 'yyyy' , '/Y' but no luck yet. 尝试了各种日期格式,例如'Y''yyyy''/Y'但还没有运气。 Does anyone know the right format? 有谁知道正确的格式?

I'm sure its not perfect. 我确定它并不完美。 But this could give you a start: 但这可以给您一个开始:

    <div id="wrapper" style="border:1px solid #333;width:200px;max-width:200px;overflow:hidden;display:block;">
        <div contenteditable="true" id="myTxt" onfocus="$(this).attr('style','padding-left:5px;min-width:20px;border:none;width:auto;display:inline;')" onblur="$(this).attr('style','width;auto;display:inline;');" style="padding-left:5px;min-width:20px;border:none;width:auto;display:inline;"></div>
        <input type="text" id="suggestions" placeholder="" onfocus="$(this).prev().focus();updateValue(this);" style="border:none; solid;width:auto;"/>
    </div>

<script type="text/javascript" src="jquery.js" ></script>
<script type="text/javascript">
function updateValue(element){
     var value = $(element).attr('placeholder');
     $('#myTxt').html( $('#myTxt').html() +value);
     $(element).attr('placeholder','');
}
$(document).bind('keyup', function(){
    showSuggesstions($('#myTxt'));
});
function showSuggesstions(element) {
    var list='';        
    var value = $(element).text();
    if(!value)
        return;
    if(value.length==4){
        $('#suggestions').attr('placeholder','');
        return;
    }
    for(var val=2000; val< 3000; val++){
        if( (val+'').indexOf(value) == 0){
            list = (val+'').substring(value.length, val.length);
            break;
        }
    }
    $('#suggestions').attr('placeholder', list);
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM