我需要使用来自 javascript 输入的值设置 asp 文本框。
我这里有:
<td ><asp:TextBox ID="AddressTextBox" runat="server" Text='<%# Bind("Address") %>' ClientIDMode="Static" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="AddressTextBox" ErrorMessage="*"
ValidationGroup="InsertCustomer" SetFocusOnError="True"></asp:RequiredFieldValidator>
</td>
我使用 here 中的自动完成地址表单
所以我将其放入 JavaScript 代码中:
var src = document.getElementById("autocomplete");
var dst = document.getElementById("AddressTextBox");
这是函数 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;
dst.value = src.value;
}
一旦选择地址,我就会尝试从自动完成功能中获取完整地址到 AddressTextBox 字段。
但我收到一个错误,AddressTextBox“当前上下文中不存在名称“AddressTextBox””
有什么想法吗?谢谢。
最佳答案
AddressTextBox 是服务器控件,因此 ID 可能会更改。 尝试如下:
var dst = document.getElementById("<%=AddressTextBox.ClientID%>").value;
这可能会解决您的错误。
关于javascript - 如何在 ASP.NET 上使用 javascript 设置文本框的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38235406/