使用javascript的下拉菜单?
嗨,我正在尝试用javascript创建下拉菜单,一旦我选择了地点,我就会在输入框中打印出来,一个是地点的纬度,另一个是经度。我的问题是,我只能将Lat或Lon输出到输入中,所以我想知道是否有办法将两者都打印出来?非常感谢。这是我的密码:使用javascript的下拉菜单?,javascript,html,drop-down-menu,Javascript,Html,Drop Down Menu,嗨,我正在尝试用javascript创建下拉菜单,一旦我选择了地点,我就会在输入框中打印出来,一个是地点的纬度,另一个是经度。我的问题是,我只能将Lat或Lon输出到输入中,所以我想知道是否有办法将两者都打印出来?非常感谢。这是我的密码: <html> <script language="JavaScript"><!-- function onChange() { var Current = document.formName3.selectNa
<html>
<script language="JavaScript"><!--
function onChange() {
var Current =
document.formName3.selectName3.selectedIndex;
document.formName3.Lat1.value =
document.formName3.selectName3.options[Current].value;
document.formName3.Lon2.value =
document.formName3.selectName3.options[Current].value;
}
//--></script>
<body>
<form
name="formName3"
onSubmit="return false;"
>
<select
name="selectName3"
onChange="onChange()"
>
<option
value=""
>
Find Place
<option
value = "52.280431"
value ="-9.686166"
>
Shop
<option
value = "52.263428"
value="-9.708326"
>
Cinema
<option
value = "52.270883"
value="-9.702414"
>
Restaurant
<option
value = "52.276112"
value="-9.69109"
>
Hotel
<option
value = "52.278994"
value="-9.6877"
>
Petrol Station
</select>
<br><br>
Lat2
<input
name="Lat1"
type="text"
value=""
>
Lon2
<input
name="Lon2"
type="text"
value=""
>
</form>
</body>
</html
找到位置
商店
电影院
餐厅
酒店
加油站
Lat2
Lon2
只有一个值,而不是两个值
而不是使用你所拥有的,而是考虑提供一个空间分隔列表作为你的选择,EX:
<!-- make sure to close all your tags as shown below -->
<option value = "52.280431 -9.686166">Shop</option>
你可以在行动中看到这一点。请注意,我通过将所有处理程序附加到window.onload处理程序而不是HTML中,使您的Javascript不那么显眼。低调的Javascript被认为是Javascript的最佳实践。给你,我给你一个解决方案
function onChange() {
var Current =
document.formName3.selectName3.selectedIndex;
var latLong = document.formName3.selectName3.options[Current].value;
// handle case where the value is ""
if (!latLong) {
document.formName3.Lat1.value = "";
document.formName3.Lon2.value = "";
return;
}
// split them on space
var latLongItems = latLong.split(" ");
// latLongItems[0] contains first value
document.formName3.Lat1.value = latLongItems[0];
// latLongItems[1] contains second
document.formName3.Lon2.value = latLongItems[1];
}