1

我尝试了不同的事情,但我无法做到这一点。我对 jquery json 和 ajax 很陌生。我编写了一个连接到我的数据库并从表中检索用户 ID 的 servlet。我写了一个 jsp,它有一个文本框,它接受输入文本并在每次按键时优化结果我想通过从我的 servlet 发送 json 对象在使用 jquery ajax 的 html 页面中做到这一点。有人可以给我一个这个场景的例子。

提前致谢。

4

1 回答 1

1

http://jsfiddle.net/tAjNz/

<input type="text" />
<select id="autoPop" multiselect="true"></select>
<script>
// define a data source (ajax or on page load);
myJson = [{value:1,text:'Item One'},{value:1,text:'Item Two'},{value:1,text:'Item Three'},{value:1,text:'Item Four'}];



$('input[type="text"]').keyup(function(){
    //Optionally update the data source when a user starts typing or use the predefined source.
    //Populate the select list
    $sel = $('select');
    $sel.html('');
    var $this = $(this);
    $.each(myJson,function(k,v){
        if(v.text.toLowerCase().indexOf($this.val().toLowerCase()) > -1) {                    
            $sel.append('<option value="' + v.value+ '">'+ v.text+'</option>');           
                }
    });
});
</script>
于 2012-11-06T18:58:08.330 回答