开发者

Passing more than 2 variables into a JQUERY autocomplete

With JQUERY autocomplete, it seems that only 'id' and 'value' are the only variables its accepting. Is there anyway to pass more to it? I am trying to pass my stateid from my cities table. Here is my setup...

Database:

+- 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 '%" . $_GE开发者_如何学CT['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);
}


In autocomplete source you can pass like this

$(any_selector).autocomplete({

        source : "url?anypara=value"
})

you can get that like : $_GET['anypara']


You can send response to you page in a format given below

[ { "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" } ]

Here value in "label" : will be shown in drop down. as shown http://img853.imageshack.us/f/74938991.png/

and you can select value from this on page by

$('input[name*="postCd"]').autocomplete({

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

to get value of "jur" :

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜