我创建了一个简单的下拉菜单,其中包含了各种功能。然后我意识到在执行functionA之后,我无法再次执行它。我必须再次执行functionA之前执行functionB(另一个函数)...我可以知道如何解决这个问题?以下是我的代码。
谢谢!
function changeForm(the_form)
{
window.location = the_form;
}
//Add Markers by Latitude and Longitude, automatically
//Listen for click
function marker()
{
google.maps.event.addListener(map, 'click', function(event) {
addMarker(event.latLng);
});
}
// Place markers in by click
function addMarker(location) {
marker = new google.maps.Marker({
position: location,
map: map,
title:"Specified Location",
icon: 'images/greenPoint.png'
});
markersArray.push(marker);
}
// Choose By Latitude and Longitude, manually
function admarker()
{
var lat = prompt("Enter latitude (-90 to 90)", "0");
while ((lat < -90) || (lat > 90))
{
alert("Latitude must be between -90 to 90");
var lat = prompt("Enter latitude (-90 to 90)", "0");
}
var lng = prompt("Enter longitude (-180 to 180)", "0");
while ((lng < -180) || (lng > 180))
{
alert("Longitude must be between -180 to 180");
var lng = prompt("Enter latitude (-180 to 180)", "0");
}
lat = parseInt(lat);
lng = parseInt(lng);
var latlng = new google.maps.LatLng(lat, lng);
var marker = new google.maps.Marker({
position: latlng,
map: map,
clickable: true,
title:"Specified Location",
icon: 'images/bluePoint.png'
})
var infiwindow = new google.maps.InfoWindow({
content: "Place where you have chosen!"
});
google.maps.event.addListener(marker,'mouseover',function(){
infiwindow.open(map,marker);
});
google.maps.event.addListener(marker,'mouseout',function(){
infiwindow.close(map,marker);
});
}
<form name = "formTwo">
<select name = "the_select" onChange = "changeForm(this.value);">
<option value = "#">- - Add Markers By Lat & Long - -</option>
<option value = "javascript:admarker()">Add Markers Manually</option>
<option value = "javascript:readMarker()">Add Markers Automatically</option>
</select></form>
答案 0 :(得分:1)
问题是,当选择框的值更改时,您的代码会运行,当然,一旦您选择了一个选项,只需再次单击它就不会更改< / em>价值。
您可以通过向this.selectedIndex = 0;
添加onChange
来解决此问题:
<select name = "the_select" onChange = "changeForm(this.value); this.selectedIndex = 0;">
...但哇是一种运行代码的复杂方式。我没有在value
属性中实际存储代码,而是建议在那里存储命令(更多的是Command pattern的样式),然后让change_form
处理将其转换为实际代码跑?这是一个极简主义的改造(live example):
<form name = "formTwo">
<select name = "the_select" onChange = "changeForm(this.value); this.selectedIndex = 0;">
<option value = "#">- - Add Markers By Lat & Long - -</option>
<option value = "adMarker">Add Markers Manually</option>
<option value = "readMarker">Add Markers Automatically</option>
</select></form>
<script type='text/javascript'>
function changeForm(the_form)
{
switch (the_form) {
case "adMarker":
// Do something
display("ad marker");
break;
case "readMarker":
// Do something else
display("read marker");
break;
}
}
</script>
答案 1 :(得分:0)
我认为如果您格式化代码并将其排列如下,它将正常工作: -
我了解您要根据下拉菜单中选择的选项更改表单。
如果我的理解是对的,我希望它对你有所帮助:
-
function changeForm(the_form) { window.location的= “HTTP:// mywebsite /” + the_form +名 “.html”; }
添加标记由Lat&amp;长 手动添加标记 自动添加标记-