loadMapAutocomplete("suggestId","searchResultPanel"); loadMapAutocomplete("suggestIdshou","searchResultPanelshou"); function loadMapAutocomplete(suggestId, searchResultPanel) { var checkValue; Ac = new BMap.Autocomplete( //建立一个自动完成的对象 { "input": suggestId , "location": "深圳" }); Ac.addEventListener("onhighlight", function (e) { //鼠标放在下拉列表上的事件 var str = ""; var _value = e.fromitem.value; var value = ""; if (e.fromitem.index > -1) { value = _value.province + _value.city + _value.district + _value.street + _value.business; } str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value; value = ""; if (e.toitem.index > -1) { _value = e.toitem.value; value = _value.province + _value.city + _value.district + _value.street + _value.business; } str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value; G(searchResultPanel).innerhtml = str; }); Ac.addEventListener("onconfirm", function (e) { //鼠标点击下拉列表后的事件 var _value = e.item.value; checkValue = _value.province + _value.city + _value.district + _value.street + _value.business; G(searchResultPanel).innerHTML = "onconfirm<br />index = " + e.item.index + "<br />myValue = " + checkValue; //model.LoadDiscAddress(_value.business); //model.LoadDiscFullAddress(checkValue); setPlace(model); });
<div class="col-sm-8" id="r-result"> <input id="suggestId" name="sendAddress" type="text" class="form-control" placeholder="街道详细(精确到门牌号) " required="required"> <p class="error">请填写正确的信息</p> </div> <div id="searchResultPanel" style="border: 1px solid #C0C0C0; width: 150px; height: auto; display: none;"></div> <div id="l-map"></div>
<div class="col-sm-8" id="r-result"> <input id="suggestIdshou" name="sendAddress" type="text" class="form-control" placeholder="街道详细(精确到门牌号) " required="required"> <p class="error">请填写正确的信息</p> </div> <div id="searchResultPanelshou" style="border: 1px solid #C0C0C0; width: 150px; height: auto; display: none;"></div> <div id="l-map"></div>
t解决了同一个页面中调用两次这个百度api方法