如何在多个输入字段上获取多个值?

时间:2012-01-11 03:01:33

标签: jquery input autocomplete

我有这样的输入框:

<input type="text" id="namecity1"  name="city1" />
<input type="hidden" id="idcity1" name="idcity1" />

<input type="text" id="namecity2"  name="city2" />
<input type="hidden" id="idcity2" name="idcity2" />

脚本:

<script src="jquery-1.4.js"></script>
<script type="text/javascript" src="jquery.autocomplete.js"></script>
<link rel="stylesheet" type="text/css" href="jquery.autocomplete.css" />

<script>
$(function() 
{
var city;
var idcity;

for(var i=1;i<=2;i++)
{
  city = "namecity"+i;  
  idcity= $("#"+city).next().attr('id');    

 $("#"+city).autocomplete("states.php").result(function(event, data, formatted) 
 {
   $("#"+idcity).val(data[1]);
 });
}//for    
});
</script> 

变量i之后:

result(function(event, data, formatted)

为什么上述的函数for不是1,2?

1 个答案:

答案 0 :(得分:2)

这应该这样做:

$( '[id^="namecity"]' ).autocomplete( 'states.php' ).result( function (e, data) {
    $( this ).next().val( data[1] );
});