我正在尝试根据用户的输入通过 xml 获取结果。
即使应该有结果,它也会不断返回“无结果”。我有一种奇怪的感觉,我刚刚忽略或错过了一些东西。
它应该由用户输入文本并自动建议地址详细信息来工作。
有什么建议么?我已经尝试调试并设置了警报,但仍然没有运气。
<input id="search" />
<div id="l-s">
<ul>
</ul>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>
<script type="text/javascript">
$(document).ready(function(){
$("#l-s li").on("click", function(){
var val = $(this);
if(val.attr("adid")){
window.location.href = "http://map.mywebsite.com/address?addressId="+val.attr("adid")+"&view=data";
}
});
$("#search").on("keypress", function(){
var _search = $("#search").val();
var _pc = "";
var _area = "";
var toSend = '<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:x="x">'+
'<soapenv:Header/>'+
'<soapenv:Body>'+
'<x:getLocalities soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'+
'<address xsi:type="xsd:string">'+_area+'</address>'+
'<state xsi:type="xsd:string"></state>'+
'<postcode xsi:type="xsd:string">'+_pc+'</postcode>'+
'</x:getLocalities>'+
'</soapenv:Body>'+
'</soapenv:Envelope>';
if(_search.length >= 2){
if(!isNaN(_search)){
_pc = _search;
}else{
_area = _search;
}
$.ajax({
type: "POST",
url: "http://map.mywebsite.com/service",
contentType: "text/xml",
dataType:"xml",
data:toSend,
success:function(output){
suggestions(output);
},
error:function(output){
suggestions(null);
}
});
}
});
});
function suggestions(input){
alert("Called");
var px = $.parseXML(input);
var xml = $(px);
var result = xml.find("Result").children('i');
if(result.length == 0){
$("#l-s ul").html("<li>No results</li>");
}else{
var results = '';
$.each(result, function(key, value){
var l = $(value);
var address = l.find("address").text();
var state = l.find("state").text();
var postCode = l.find("postcode").text();
var code = l.find("addressId").text();
results = results + '\n' + '<li adid="'+code+'">'+address + ", "+state+" "+postCode+'</li>';
});
$("#l-s ul").html(results);
}
$("#l-s").css("display", "inline");
}
</script>