Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ajax列表可用用户javascript中的小错误_Javascript_Html_Ajax_Jquery - Fatal编程技术网

ajax列表可用用户javascript中的小错误

ajax列表可用用户javascript中的小错误,javascript,html,ajax,jquery,Javascript,Html,Ajax,Jquery,我有这个javascript/jquery来列出可用的用户 HTML: jquery: <script type="text/javascript"> function lookup(username) { if(username.length == 0) { // Hide the suggestion box. $('#suggestions').hide(); } else { $.post("php/rpc.php"

我有这个javascript/jquery来列出可用的用户

HTML:


jquery:

<script type="text/javascript">
function lookup(username) {
    if(username.length == 0) {
    // Hide the suggestion box.
        $('#suggestions').hide();
    } else {
        $.post("php/rpc.php", {queryString: ""+username+""}, function(data){
            if(data.length >0) {
                $('#suggestions').show();
                $('#autoSuggestionsList').html(data);
            }
        });
    }
} // lookup

function fill(thisValue) {
    $('#username').val(thisValue);
    setTimeout("$('#suggestions').hide();", 200);
}
</script>

函数查找(用户名){
如果(username.length==0){
//隐藏建议框。
$(“#建议”).hide();
}否则{
$.post(“php/rpc.php”,{queryString:“+username+”},函数(数据){
如果(data.length>0){
$(“#建议”).show();
$('#autoSuggestionsList').html(数据);
}
});
}
}//查找
函数填充(thisValue){
$('#username').val(thisValue);
setTimeout(“$('#建议”).hide();”,200);
}
这将提供一个可用用户名列表,您可以键入这些用户名

HTML示例:

<li onclick="fill('user1');">user1</li>
<li onclick="fill('user1');">user1</li>
用户1 user1
但是fill函数不起作用。如果我添加
alert('test')
会出现一个弹出窗口,因此它可以工作,但它不会填充输入字段
username

,这里的主要错误是
input
元素的
name
属性与
id
属性不同。或者

<input name="username" type="text" size="16" onkeyup="lookup(this.value);" onblur="fill();"/>
这消除了对两个函数的需要,并使使用AJAX加载的HTML更简单,因为您只需加载

<li>user1</li>
<li>user2</li>
  • 用户1
  • 用户2

  • 有关此代码的完整工作示例,请参阅。

    不要将字符串传递给
    setTimeout
    。另外,{queryString:“+username+”}应该更像是在黑暗中拍摄的{queryString:“+username+”},但是在第一次调用
    fill()
    之后,是否有什么东西会导致
    模糊?这将调用不带参数的fill,将输入清空。如果删除
    onblur
    处理程序,问题是否仍然存在?
    <input id="username" name="username" type="text" size="16" onkeyup="lookup(this.value);" onblur="fill();"/>
    
         $.post("php/rpc.php", {queryString: username}, function(data){
            if(data.length >0) {
                $('#suggestions').show();
                $('#autoSuggestionsList').html(data)
                    // find list elements and attach the event handler
                    .find('li').click(function() {
                        // this removes the need to pass in any arguments:
                        // just use the text of the list element
                        var val = $(this).text();
                        $('#username').val(val);
                        // as @SLaks notes, better a function than a string here
                        setTimeout(function() {
                            $('#suggestions').hide();
                        }, 200);
                    });
            }
        });
    
    <li>user1</li>
    <li>user2</li>