jQuery自动完成 - 覆盖源数据中的标签/值

时间:2011-01-03 15:01:51

标签: php mysql jquery-ui

自动完成窗口小部件似乎要求数据在数组中包含变量'label'和'value'。

是否可以覆盖它并使用您自己的值(例如数据库中的列),这样您可以在表单中拥有多个带自动填充功能的文本框?

使用PHP和MySQL自动完成功能的任何人的一些提示......

while($r = mysql_fetch_assoc($rsCustomerLookup)) {
$rows[] = $r;
}
print json_encode($rows);

从数据库中获取json格式的数据

select: function( event, ui ) { 
    $( "#name" ).val(ui.item.name);
    $( "#surname" ).val(ui.item.surname);
    $( "#company" ).val(ui.item.company);
    $( "#address1" ).val(ui.item.address1);
    $( "#address2" ).val(ui.item.address2);
     etc
如果您的目标是从一个自动填充文本框填充整个表单,则

用数据填充其他文本框

2 个答案:

答案 0 :(得分:0)

您可以做的是将value设置为记录的数据库ID。然后,您可以获取value(这是数据库ID)并获取namesurnamecompany等。

答案 1 :(得分:0)

enter link description here

<script> 
$().ready(function() {
$('#tag').autocomplete('tag.php?find=tag', {
          width: 260,
          matchContains: true,
          selectFirst: false
        });
});

    $('#other').autocomplete('tag.php?find=other', {
          width: 260,
          matchContains: true,
          selectFirst: false
        });
});
</script>
<?php
//in tag.php
$find = $_GET["find"];
if($find=='tag'){
    $q = strtolower($_GET["q"]);
    if (!$q) return;
    $sql = "select DISTINCT tag from tag where name_tag LIKE '%$q%'";
    $rsd = mysql_query($sql);
    while($rs = mysql_fetch_array($rsd)) {
        $cname = $rs['name_tag'];
        echo "$cname\n";
    }
}
/*
This adds to other data
    if($find=='other'){
    $q = strtolower($_GET["q"]);
    if (!$q) return;
    $sql = "select DISTINCT tag from tag where name_tag LIKE '%$q%'";
    $rsd = mysql_query($sql);
    while($rs = mysql_fetch_array($rsd)) {
        $cname = $rs['name_tag'];
        echo "$cname\n";
    }
}
*/
?>
<input id='tag' type='text'>
<input id='other' type='text'>