我正在尝试填充HTML。带有selectlist
的下拉列表,其中填充了数据库调用的字符串值(位置地址)和文本(位置描述)字段。我将选择列表作为viewdata传递给我的视图。下拉列表填充正常,但是当我使用该值时,它为null或为空,如我在javascript函数中输入的警报所示。以下是代码为this.locations.value
为空的任何想法:
我的观看代码:
<script type="text/javascript">
var map;
var gdir;
var geocoder = null;
var addressMarker;
function setDirections(fromAddress, toAddress, locale) {
alert(toAddress);
gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale });
}
</script>
<div id="maincontent2">
<form action="#" onsubmit="setDirections(this.from.value, this.locations.value, 'en_US'); return false">
<table>
<tr>
<th align="left">From: </th>
<td align="left" ><input type="text" id="fromAddress" name="from" size="35px" value="King of Prussia, PA"/></td>
<th align="left"> To: </th>
<td align="left"> <%= Html.DropDownList("locations",(SelectList)ViewData["OfficeLocations"])%></td>
</tr>
<tr>
<td></td>
<td align="left">
<br />
<input name="submit" type="submit" value="Get Directions!" />
</td>
</tr>
</table>
<table>
<tr>
<td valign="top"><div id="drv_directions" style="width: 250px"></div></td>
<td valign="top" style="padding-top:15px"><div id ="map_canvas"></div></td>
</tr>
</table>
</form>
</div>
我的控制器代码:
public ActionResult Directions()
{
uls_dbDataContext ulsdb_dc = new uls_dbDataContext();
ViewData["OfficeLocations"] = new SelectList(ulsdb_dc.GetOfficeLocations(),"location_address", "location_name");
ViewData["Title"] = "Directions";
return View();
}
答案 0 :(得分:0)
位置是一个选择,没有值属性。要获取所选选项的值,您需要使用所选索引,找到正确的选项,并引用该选项的值。但是,如果使用jQuery,它将使用jQuery对象的val()方法获取select的值。我建议使用jQuery,因为它会使代码更简单,MS将使用Visual Studio支持它。
使用jQuery的示例:
<script type='text/javascript'>
$(document).ready( function() {
$('form').submit( function() {
var fromAddress = $(this).find('#from').val();
var toAddress = $(this).find('#locations').val();
var locale = 'en-US';
....
return false;
});
});
</script>
<form action="#">
<table>
<tr><th align="left">From: </th>
<td align="left" ><input type="text" id="fromAddress" name="from" size="35px"
value="King of Prussia, PA"/></td>
<th align="left"> To: </th>
<td align="left"> <%= Html.DropDownList("locations",(SelectList)ViewData["OfficeLocations"])%></td>
...
答案 1 :(得分:-3)
以下代码正在运行:
<script type="text/javascript">
var map;
var gdir;
var geocoder = null;
var addressMarker;
function setDirections(fromAddress, toAddress, locale) {
gdir.load("from: " + fromAddress + " to: " + toAddress,
{ "locale": locale });
}
$(document).ready(function() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map_canvas"));
gdir = new GDirections(map, document.getElementById("drv_directions"));
GEvent.addListener(gdir, "load", onGDirectionsLoad);
GEvent.addListener(gdir, "error", handleErrors);
setDirections("King of Prussia", "1302 Conshohocken Road, Conshohocken, PA 19428", "en_US");
}
$('form').submit(function() {
var fromAddress = $(this).find('#from').val();
var toAddress = $(this).find('#locations').val();
var locale = 'en-US';
alert(fromAddress);
alert(toAddress);
setDirections(fromAddress, toAddress, locale);
return false;
});
});
</script>