【发布时间】:2014-07-12 17:47:18
【问题描述】:
我已经在 Stackoverflow 和整个网络上搜索了这个问题的答案。我找到了一些很好的建议(例如http://af-design.com/blog/2010/05/12/using-jquery-uis-autocomplete-to-populate-a-form/),但无法让任何工作......完全是由于我的无知!
我有一个 JSON 文件,其中包含地区、州和邮政编码数据(缩短版):
[
{
"PCODE":7255,
"LOCALITY":"LOCCOTA",
"STATE":"TAS"
},
{
"PCODE":7255,
"LOCALITY":"LUGHRATA",
"STATE":"TAS"
},
{
"PCODE":7255,
"LOCALITY":"MEMANA",
"STATE":"TAS"
}
]
基本上,我希望允许用户在表单字段中输入 Locality,然后让 jQuery 搜索 JSON 文件,找到 Postcode 和 State 的匹配项,并使用这些匹配值填充 Postcode 和 State 表单文本字段
这是我正在使用的表单以及从http://af-design.com/ 提取的一些测试 jQuery(我无法开始工作 - 是我的错,不是源脚本):
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<style>
label{
float:left;
width:80px;
}
</style>
<link rel="stylesheet" href="http://static.jquery.com/ui/css/base2.css" type="text/css" media="all" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
var ac_config = {
source: "p-codes.json",
select: function(event, ui){
$("#city").val(ui.item.LOCALITY);
$("#state").val(ui.item.STATE);
$("#zip").val(ui.item.PCODE);
},
minLength:1
};
$("#city").autocomplete(ac_config);
});
</script>
</head>
<body>
<form action="#" method="post">
<p><label for="city">City</label><br />
<input type="text" name="city" id="city" value="" /></p>
<p><label for="state">State</label><br />
<input type="text" name="state" id="state" value="" /></p>
<p><label for="zip">Zip</label><br />
<input type="text" name="zip" id="zip" value="" /></p>
</form>
</body>
</html>
任何帮助或建议将不胜感激!
问候,
湄公河
【问题讨论】:
标签: jquery json forms autocomplete