javascript - 如何在 ASP.NET 上使用 javascript 设置文本框的值

标签 javascript c# jquery asp.net google-maps

我需要使用来自 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/

相关文章:

javascript - 根据数值对嵌套数组进行排名

javascript - 带有 Material UI 自动完成功能的 React-final-form

javascript - 预检响应无效(重定向),浏览器继续使用 OPTION 方法

javascript - 严不严

javascript - 这是使用 React.Suspense 的正确方法吗?

c# - 我如何从 asp.net web 应用程序中选择文件夹或文件?

c# - 为什么C#中的多线程快速排序比单线程慢

javascript - 有什么方法可以知道用户是否接到电话? (Javascript)

c# - 包含两项并且可以通过另一项返回一项的数据结构列表

javascript - 如何在 jQuery Mobile 中设置宽度为 100% 的输入类型日期