1

我有一个包含 2 个字段“什么”和“区域”的表单。当有 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" ) 无济于事。

4

1 回答 1

0

最简单的方法是加入 2 个数组并对其中一个数组使用自动完成功能。这是jsfiddle 但您可以使用jqueryui 自动完成的属性:

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

您可以在这里查看如何使用它

于 2013-08-01T15:40:41.863 回答