从头开始自动完成文本框

时间:2014-08-04 07:38:39

标签: javascript jquery html css

我正在使用javascript和jquery从头开始编写自动完成输入控件。我知道我可以使用jquery自动完成功能。但那时就不会有任何乐趣了。

我到目前为止的自动填充功能显示了建议,但我无法使用鼠标选择建议。我怎么能这样做?

我到目前为止的代码是关注html,

<div class="sug">
<input type="text" id="auto" onkeyup="display(event)" onblur="hide()" autocomplete="off" />
<div class="suggestion" id="suggestion">
</div>
</div>

以下是样式表

<style>
.suggestion
{
    border:solid 2px black;
}
.sug > .suggestion
{
    display:none;
}
.sug > .suggestion,#auto
{
    width:100px;
}
}
</style>

我的javascript代码正在关注

<script type="text/javascript">


    var array = new Array();
    array.push("heena");
    array.push("bhawin");
    array.push("aruna");
    array.push("mahesh");
    array.push("anshul");
    array.push("jagruti");
    array.push("neha");
    array.push("sherry");
    array.push("sonali");
    var data;
    var id; //for providing id to each div
    function display(e)
    {


        if (e.keyCode == 38 || e.keyCode == 40)
        {
            selectit(e);
        }
        data = "";
        id = 0;
        var state = $('#auto').val();
        if (state == "")//value empty
        {
            $('.suggestion').css({ display: "none" });
        }
        else
        {
            for (var i = 0; i < array.length; i++)
            {
                var key = 0;
                for (j = 0; j < state.length; j++)
                {
                    //for the matching of the array element with the text in the textbox
                    if (array[i][j] == state[j])
                    {
                        key++;
                    }
                }
                //putting the matched array element in a div
                if (key == state.length)
                {
                    //the whole array will be copied with the bold values inserted
                    var bolde = "";
                    for (var k = 0; k < key; k++)
                    {
                        bolde += "" + array[i][k] + "";
                    }
                    for (var l = key; l < array[i].length; l++)
                    {
                        bolde += array[i][l];
                    }
                    id++;
                    data += "<div id='" + id + "'>" + bolde + "</div>";
                }
            }
            $('.suggestion').html(data);
            $('.suggestion').css({ display: "block" });
            selectit(e);
            if (data == "")
            {
                $('.suggestion').css({ display: "none" });
            }
        }
    }
    function hide()
    {
        $('#suggestion').css({ display: "none" }); ;
    }
    function selectit(e)
    {
        var child = document.getElementById("suggestion").childNodes;

        for (var i = 0; i < child.length; i++)
        {

            if (child[i].id == "1")
            {
                child[i].style.color = "red";   //here is the problem in the code
            }
        }
    }
</script>

由于

1 个答案:

答案 0 :(得分:0)

有两个问题

ONE: 您需要从hide();

取消onblur
<div class="sug">
<input type="text" id="auto" onkeyup="display(event)" autocomplete="off" /><!--i changed here to Note the on blur-->
<div class="suggestion" id="suggestion">
</div>
</div>

TWO:设置div的onclick并在其中使用hide();

<script>
 var array = new Array();
    array.push("heena");
    array.push("bhawin");
    array.push("aruna");
    array.push("mahesh");
    array.push("anshul");
    array.push("jagruti");
    array.push("neha");
    array.push("sherry");
    array.push("sonali");
    var data;
    var id;
    function display(e)
    {


        if (e.keyCode == 38 || e.keyCode == 40)
        {
            selectit(e);
        }
        data = "";
        id = 0;
        var state = $('#auto').val();
        if (state == "")
        {
            $('.suggestion').css({ "display": "none" });
        }
        else
        {
            for (var i = 0; i < array.length; i++)
            {
                var key = 0;
                for (j = 0; j < state.length; j++)
                {
                    if (array[i][j] == state[j])
                    {
                        key++;
                    }
                }
                if (key == state.length)
                {
                    var bolde = "";
                    for (var k = 0; k < key; k++)
                    {
                        bolde += "" + array[i][k] + "";
                    }
                    for (var l = key; l < array[i].length; l++)
                    {
                        bolde += array[i][l];
                    }
                    id++;
                    data += '<div id="' + id + '" onclick="$(\'#auto\').val(this.innerHTML);hide();" >' + bolde + "</div>";// i changed here note the on click
                }
            }
            $('.suggestion').html(data);
            $('.suggestion').css({ "display": "block" });
            selectit(e);
            if (data == "")
            {
                $('.suggestion').css({ "display": "none" });
            }
        }
    }
    function hide()
    {
        $('#suggestion').css({ "display" : "none" }); ;
    }
    function selectit(e)
    {
        var child = document.getElementById("suggestion").childNodes;

        for (var i = 0; i < child.length; i++)
        {

            if (child[i].id == "1")
            {
                child[i].style.color = "red"; 
            }
        }
    }

</script>