我有一个搜索框,在搜索地址时会抛出某些自动建议的值。
现在有很多方法可以触发搜索事件:
在这种情况下,有没有办法可以判断是否在事件处理程序中选择了 Enter 键?
var esriMap = new Map("esriMap", {
basemap: "topo",
center: [-12.4260, 31.3403],
zoom: 12
});
var search = new Search({
map: esriMap,
}, dom.byId("esriSearch"));
search.startup();
search.on("select-result", searchboxResult); // the event handler
function searchboxResult(e)
{
// determine "Enter" key versus "Selection from auto-suggest"
}
答案 0 :(得分:1)
当然,有一种方法可以确定在地图上显示选择时是否进行了选择或按下了Enter键。
以下是实现此目的的工作代码 -
require([
"esri/map",
"esri/dijit/Search",
"dojo/on",
"dojo/domReady!"
], function (Map, Search, on) {
var map = new Map("map", {
basemap: "gray",
center: [-120.435, 46.159], // lon, lat
zoom: 7
});
var search = new Search({
map: map
}, "search");
search.startup();
var isEnter= false;
on(search.inputNode, "keypress", function(evt){
isEnter = evt.keyCode == 13;
});
search.on("select-result", searchboxResult);
function searchboxResult(e)
{
alert(isEnter?"By Enter Selection": "By Suggestion Selection");
// determine "Enter" key versus "Selection from auto-suggest"
}
});

html,
body,
#map {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
#search {
display: block;
position: absolute;
z-index: 2;
top: 20px;
left: 74px;
}

<link rel="stylesheet" href="https://js.arcgis.com/3.18/esri/themes/calcite/dijit/calcite.css">
<link rel="stylesheet" href="https://js.arcgis.com/3.18/esri/themes/calcite/esri/esri.css">
<script src="https://js.arcgis.com/3.18/"></script>
<body class="calcite">
<div id="search"></div>
<div id="map"></div>
</body>
&#13;
希望这会对您有所帮助:)