我有一个带有2个字段“ what”和“ area”的表格。当有2个数组时,我可以自动完成工作,每个字段一个,但是我需要第一个字段“ what”来使用2个不同数组中的数据。这可能吗?

//This works
<form........
<input name="what" id="what" type="text" size="45" />
<input name="area" id="area" type="text" size="45" />
......form/>

<script>
var tags = [ <?php do { ?>"<?php echo $row_rsCategories['category']; ?>",<?php } while ($row_rsCategories = mysql_fetch_assoc($rsCategories)); ?> ];
$( "#what" ).autocomplete({
source: function( request, response ) {
var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
response( $.grep( tags, function( item ){
return matcher.test( item );
}) );
}
});
</script>
<script>
var tags3 = [ <?php do { ?>"<?php echo $row_rsDirectory['suburb']; ?>, <?php echo $row_rsDirectory['postcode']; ?> ",<?php } while ($row_rsDirectory = mysql_fetch_assoc($rsDirectory)); ?> ];
$( "#area" ).autocomplete({
source: function( request, response ) {
var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
response( $.grep( tags3, function( item ){
return matcher.test( item );
}) );
}
});
</script>


//这不起作用

<form........
<input name="what" id="what" type="text" size="45" />
<input name="area" id="area" type="text" size="45" />
......form/>

<script>
var tags = [ <?php do { ?>"<?php echo $row_rsCategories['category']; ?>",<?php } while ($row_rsCategories = mysql_fetch_assoc($rsCategories)); ?> ];
$( "#what" ).autocomplete({
source: function( request, response ) {
var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
response( $.grep( tags, function( item ){
return matcher.test( item );
}) );
}
});
</script>
<script>
var tags2 = [ <?php do { ?>"<?php echo $row_rsKeywords['keyword']; ?>",<?php } while ($row_rsKeywords = mysql_fetch_assoc($rsKeywords)); ?> ];
$( "#what" ).autocomplete({
source: function( request, response ) {
var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
response( $.grep( tags2, function( item ){
return matcher.test( item );
}) );
}
});
</script>
<script>
var tags3 = [ <?php do { ?>"<?php echo $row_rsDirectory['suburb']; ?>, <?php echo $row_rsDirectory['postcode']; ?> ",<?php } while ($row_rsDirectory = mysql_fetch_assoc($rsDirectory)); ?> ];
$( "#area" ).autocomplete({
source: function( request, response ) {
var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
response( $.grep( tags3, function( item ){
return matcher.test( item );
}) );
}
});
</script>


我尝试将表单字段id =“ what”更改为class =“ what”,并将$(“ #what”)更改为$(“ .what”)无济于事。

最佳答案

最简单的方法是联接2个数组,并对结果数组之一使用自动完成功能。
这是jsfiddle
但是您可以使用jqueryui自动完成的source属性:

.autocomplete({source: function(request, response){
    var res = getDataBySearchTerm( request.term );
    response( res );
}});


您可以查看如何使用它here

关于javascript - 尝试使用来自2个不同数组的自动完成功能填充表单字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17993186/

10-12 00:58