我的表单中有两个隐藏的输入字段:
<input type="hidden" name="lat" id="lat" value=""/>
<input type="hidden" name="long" id="long" value="" />
我通过执行以下操作来分配它们的值:
document.getElementById('lat').value = lat;
document.getElementById('long').value = lng;
谁能告诉我如何删除隐藏的 <input>
字段并将它们替换为 @Html.HiddenFor<>
并让我的 Javascript 更新 HiddenFor?我想这样做是因为它显然会自动绑定(bind)数据。
我的 HiddenFor 目前看起来像这样:
@Html.HiddenFor(m => Model.Listing.Location.Latitude);
@Html.HiddenFor(m => Model.Listing.Location.Longitude);
我更改 Javascript 来执行此操作:
document.getElementById('Listing.Location.Latitude').value = lat;
document.getElementById('Listing.Location.Longitude').value = lng;
我在控制台中收到以下错误:
Uncaught TypeError: Cannot set property 'value' of null
谁能看出我错在哪里?
最佳答案
这些字段的 ID 将带有下划线而不是点,它们的名称将带有点。试试这个:
document.getElementById('Listing_Location_Latitude').value = lat;
关于javascript - ASP.NET MVC 3 HiddenFor Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12705166/