根据指定长度的输入文本动态填充数据库中的选择框

时间:2012-06-08 19:56:32

标签: javascript jquery html

我需要能够根据输入文本值动态填充html选择。一旦文本框值为8个字符以上,就需要自动触发。用户不必选项卡或单击以查看填充值。这是我到目前为止所做的,但只有在文本框失去焦点时才会发生火灾。

//Html code
<input id=InputText name=InputText onblur="populateListBox();" onchange="CheckInputLength();" onkeydown="CheckInputLength();" value="" onfocus="this.select();">

// Javascript代码

function CheckInputLength()
{
    if($("#InputText").val().length >= 8)
        populateListBox();
}

function populateListBox()
{   
            $.get("???", function( data ) {
                $('#listBox').children().remove();
                var options = data;

                $("#listBox").append( options );
                $("#listBox").html( data );

            });

}

2 个答案:

答案 0 :(得分:0)

我会查看Select2,听起来它具有您想要的所有功能,还有一些:http://ivaynberg.github.com/select2/

$("#InputText").select2({
  minimumInputLength: 8,
  ajax: {
    url: "???",
    // your other options
  }
});

答案 1 :(得分:0)

$('#InputText').keypress(function (event) {
    var $inputText = $(this);
    // Before keypress.
    setTimeout(function () {
        // After keypress.
        if ($inputText.val().length >= 8) { populateListBox(); }
    }, 0);
});