从网上下载了一个jquery自动完成功能。但是,原代码,只能对一个input实现自动完成,而我是要实现3个,其后台的php处理返json我能看会,就是不会jquery.如何修改能实现对3个input自动完成呢?因为3个input的id不能一样。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script type="text/javascript" src="jquery.autocomplete.js"></script>
<script type="text/javascript" src="jquery.autocomplete.pack.js"></script>
<link rel="stylesheet" type="text/css" href="jquery.autocomplete.css" />
<script language="javascript">
$(document).ready(function(){
$("#payee_name").autocomplete(    
            "getindex.php",    
            {    
                delay:10,    
                minChars:1,    
                matchSubset:1,    
                matchContains:1,    
                cacheLength:10,    
                onItemSelect:selectItem,    
                onFindValue:findValue,    
                formatItem:formatItem,    
                autoFill:false    
            }    
            );    
               
            function findValue(li) {    
                if( li == null ) return alert("No match!");    
                if( !!li.extra ) var sValue = li.extra[0];    
                else var sValue = li.selectValue;  alert("1");  
                }    
            function selectItem(li) { findValue(li);alert("2"); }    
            function formatItem(row) {return row[0];//return row[0] + " (id: " + row[1] + ")"//如果有其他参数调用row[1],对应输出格式Sparta|896    
            }    
            function lookupAjax(){ 
            var oSuggest = $("#payee_name")[0].autocompleter;    
            oSuggest.findValue();    
            return false;    
            }
});
</script>
<title>自动完成测试</title>
</head>
<body>
<input type="text" name="keyword" id="payee_name" size="30" />
<input type="text" name="keyword" id="payee_name2" size="30" />
<input type="text" name="keyword" id="payee_name3" size="30" />
</body>
</html>
难道要把中间的<script></script>复制3次,再分别修改其id中的payee_name,求方案。谢谢。

解决方案 »

  1.   

    用这个 $('ipnut[name=keyword]')对象就可以控制这三个input了,可以去看一下jquery选择器,很牛逼的,各种方法获取
      

  2.   

    也可以用jQuery.each   http://api.jquery.com/jQuery.each/$('input').each(function(){
    $(this).autocomplete(    
                "getindex.php",    
                {    
                    delay:10,    
                    minChars:1,    
                    matchSubset:1,    
                    matchContains:1,    
                    cacheLength:10,    
                    onItemSelect:selectItem,    
                    onFindValue:findValue,    
                    formatItem:formatItem,    
                    autoFill:false    
                });  
     });
      

  3.   


    $("#payee_name").autocomplete(...
    换成$('ipnut[name=keyword]').autocomplete(...
      

  4.   

    学jquery得看api,把全部api中的练习写一遍就会了
      

  5.   


    不好意思, 原来的 $('ipnut 写错了, 应该是 $('input[name=keyword]').autocomplete(...推荐你用个更方便的写法:
    $(":text[name=keyword]").autocomplete(...后面的你自己复制吧……