【发布时间】:2015-10-06 12:45:31
【问题描述】:
大家好,我是java脚本的新手。所以我希望你能帮助我。当用户将数据设置为城市字段时,我想自动在国家字段中设置数据。 我有一个 xml 文件:
<ROWSET>
<ROW>
<city></city>
<country></country>
</ROW>
</ROWSET>
在 html 文档中我有两个输入字段
<input id="id_city">
<input id="country">
这是我的js代码:
$.ajax({
url: "/media/xml/country.xml", //your url
type: "GET", //may not need this-fiddle did-default is GET
dataType: "xml",
success: function(xmlResponse) {
var data = $("ROW", xmlResponse).map(function() {
return {
value: $("city", this).text() + ", " + ($.trim($("country", this).text()) || "(unknown country)")
};
}).get();
$("#id_city").autocomplete({
source: function(req, response) {
var re = $.ui.autocomplete.escapeRegex(req.term);
var matcher = new RegExp("^" + re, "i");
response($.grep(data, function(item) {
return matcher.test(item.value);
}));
},
minLength: 2,
select: function(event, ui) {
$("p#result").html(ui.item ? "Selected: " + ui.item.value + ", cityId: " + ui.item.id : "Nothing selected, input was " + this.value);
}
});
}
});
我不知道如何将数据自动设置为国家字段 谢谢。
【问题讨论】:
-
还有什么问题?
-
对不起,我不知道如何将数据自动设置到国家字段
标签: javascript jquery jquery-ui autocomplete jquery-ui-autocomplete