我有自动填充 javascript 输入(使用谷歌地图) 我想将自动完成输入的值传递给aspx.cs代码,我想将其转移到字符串命名地址,请帮助我,我在这个问题上突破了3天以上。
aspx页面:
<
div id="locationField">
**<input id="autocomplete"** placeholder="הקלד את הכתובת"
onFocus="geolocate()" type="text" dir="rtl"></input>
</div>
<script>
var placeSearch, autocomplete;
var componentForm = {
street_number: 'short_name',
route: 'long_name',
locality: 'long_name',
administrative_area_level_1: 'short_name',
country: 'long_name',
postal_code: 'short_name'
};
function initAutocomplete() {
// Create the autocomplete object, restricting the search to geographical
// location types.
autocomplete = new google.maps.places.Autocomplete(
/** @type {!HTMLInputElement} */(document.getElementById('autocomplete')),
{ types: ['geocode'] });
var hdnfldVariable = document.getElementById("<%= hdnfldVariable.ClientID %>");
}
function somefunction() {
//set this if you have dynamic ClientID
var hdnfldVariable = document.getElementById("autocomplete").value.toString();
autocomplete.addListener('place_changed', fillInAddress);
}
function fillInAddress() {
// Get the place details from the autocomplete object.
var place = autocomplete.getPlace();
for (var component in componentForm) {
document.getElementById(component).value = '';
document.getElementById(component).disabled = false;
}
autocomplete.getPlace().address_components
// Get each component of the address from the place details
// and fill the corresponding field on the form.
for (var i = 0; i < place.address_components.length; i++) {
var addressType = place.address_components[i].types[0];
if (componentForm[addressType]) {
var val = place.address_components[i][componentForm[addressType]];
document.getElementById(addressType).value = val;
}
}
}
// Bias the autocomplete object to the user's geographical location,
// as supplied by the browser's 'navigator.geolocation' object.
function geolocate() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var geolocation = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
var circle = new google.maps.Circle({
center: geolocation,
radius: position.coords.accuracy
});
autocomplete.setBounds(circle.getBounds());
});
}
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key---&libraries=places&callback=initAutocomplete"
async defer></script>
<asp:HiddenField ID="hdnfldVariable" runat="server" />
<asp:Button ID="ButtonAddEvent" runat="server" Text="Add"
Font-Bold="True" Font-Names="Gisha" Font-Size="17pt" ForeColor="Blue"
onclick="ButtonAddEvent_Click"
OnClientClick="somefunction();"
style="z-index: 1; left: 497px; top: 774px; position: absolute" />
</asp:Content>
后面的代码(将输入值传递给此处):
protected void ButtonAddEvent_Click(object sender, EventArgs e)
{
string Event_Address = hdnfldVariable.ToString();
}
请帮助我,这是非常重要的项目,非常感谢你:)。
答案 0 :(得分:1)
对此javascript函数进行以下更改。
function somefunction() {
//set this if you have dynamic ClientID
document.getElementById("<%= hdnfldVariable.ClientID %>").value = document.getElementById("autocomplete").value.toString();
autocomplete.addListener('place_changed', fillInAddress);
}
比对后面代码的更改:
string Event_Address = hdnfldVariable.Value.ToString();