2011-04-21 212 views
1

嗨,我想创建JavaScript的下拉菜单,一旦我选择的地方我打印输出框中的一个将纬度的地方,在第二我会得到经度。我的问题是,我只能做到这一点输出拉或Lon到输入,所以我想知道有没有办法做到这一点,让他们都打印?谢谢。这里是我的代码:下拉菜单与JavaScript?

<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 

回答

3

Option只有一个值,而不是两个。

而不是使用你有什么,而不是考虑提供一个空格分隔列表,您的选择,例如:

<!-- make sure to close all your tags as shown below --> 
<option value = "52.280431 -9.686166">Shop</option> 

然后,你将需要打破他们在你的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]; 
} 

你可以在行动here中看到这一点。请注意,我通过在window.onload处理程序中附加所有处理程序而不是在HTML中使您的Javascript变得不那么突兀。不显眼的Javascript被认为是Javascript的最佳实践。

+0

这项工作就像我需要它非常感谢你:) – Sasha 2011-04-21 13:22:44