将两个以上的变量传递到JQUERY自动完成中

时间:2011-03-18 03:01:44

标签: jquery mysql database

使用JQUERY自动完成功能,似乎只有'id'和'value'是它接受的唯一变量。反正有没有传递给它?我试图从我的城市表中传递我的stateid。这是我的设置...

数据库:

+- States
stateid
state

+- Cities
cityid
stateid
city

SCRIPT QUERIED:

$fetch = mysql_query("SELECT 'state' AS stateid, state FROM states WHERE state like '%" . $_GET['term'] . "%' UNION ALL SELECT 'city' AS cityid, city, stateid AS citystateid FROM cities where city like '%" . $_GET['term'] . "%'");

/* Retrieve and store in array the results of the query.*/

while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {

    $row_array['id'] = $row['stateid'];
    $row_array['value'] = $row['state'];
    $row_array['citystateid'] = $row['citystateid'];

    array_push($return_arr,$row_array);
}

2 个答案:

答案 0 :(得分:0)

在自动填充源中,您可以像这样传递

$(any_selector).autocomplete({

        source : "url?anypara=value"
})

你可以这样:$ _GET ['anypara']

答案 1 :(得分:0)

您可以使用下面给出的格式向您发送回复

[ { "city" : "ALCOVE", "countyNm" : "ALBANY", "jur" : "NY", "postCd" : "12007", "label" : "12007 - ALCOVE - ALBANY","value" : "12007" },
{ "city" : "ALPLAUS", "countyNm" : "SCHENECTADY", "jur" : "NY", "postCd" : "12008", "label" : "12008 - ALPLAUS - SCHENECTADY","value" : "12008" },
{ "city" : "REXFORD", "countyNm" : "SCHENECTADY", "jur" : "NY", "postCd" : "12008", "label" : "12008 - REXFORD - SCHENECTADY","value" : "12008" },
{ "city" : "SCHENECTADY", "countyNm" : "SCHENECTADY", "jur" : "NY", "postCd" : "12008", "label" : "12008 - SCHENECTADY - SCHENECTADY","value" : "12008" },
{ "city" : "ALTAMONT", "countyNm" : "ALBANY", "jur" : "NY", "postCd" : "12009", "label" : "12009 - ALTAMONT - ALBANY","value" : "12009" },
{ "city" : "THOMPSONS LAKE", "countyNm" : "ALBANY", "jur" : "NY", "postCd" : "12009", "label" : "12009 - THOMPSONS LAKE - ALBANY","value" : "12009" },
{ "city" : "ALLENDALE", "countyNm" : "BERKSHIRE", "jur" : "MA", "postCd" : "1201", "label" : "1201 - ALLENDALE - BERKSHIRE","value" : "1201" },
{ "city" : "PITTSFIELD", "countyNm" : "BERKSHIRE", "jur" : "MA", "postCd" : "1201", "label" : "1201 - PITTSFIELD - BERKSHIRE","value" : "1201" },
{ "city" : "PERTH", "countyNm" : "MONTGOMERY", "county" : "57", "countryNm" : "USA", "country" : "USA", "jur" : "NY", "postCd" : "12010", "label" : "12010 - PERTH - MONTGOMERY","value" : "12010" },
{ "city" : "WEST CHARLTON", "countyNm" : "MONTGOMERY", "jur" : "NY", "postCd" : "12010", "label" : "12010 - WEST CHARLTON - MONTGOMERY","value" : "12010" } ]

此处“label”中的值:将显示在下拉列表中。如图所示http://img853.imageshack.us/f/74938991.png/

您可以通过

从页面中选择值
$('input[name*="postCd"]').autocomplete({

 select : function(event, ui) {
    $('anyselector').val(ui.item.jur);
 }
});

获得“jur”的价值: