简体   繁体   English

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

[英]How to set the value for textbox with javascript on an ASP.NET

I need to set up asp textbox with a value from javascript input. 我需要使用来自javascript输入的值设置asp文本框。

i have here: 我在这里:

<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>

I use autocomplete address form from here 我从这里使用自动填写地址表格

so i placed this to the javascript code: 所以我将其放置到javascript代码中:

var src = document.getElementById("autocomplete");
var dst = document.getElementById("AddressTextBox");

and this to function fillInAddress() funcion: 这是功能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;
        }

Im trying to get the full address from the autocomplete to the AddressTextBox field as soon as the address is selected. 我试图在选择地址后立即从自动完成功能获取完整地址到AddressTextBox字段。

But im getting an error that AddressTextBox "The name 'AddressTextBox' does not exist in the current context" 但是我收到一个错误,指出AddressTextBox“名称'AddressTextBox'在当前上下文中不存在”

Any ideas? 有任何想法吗? thanks. 谢谢。

AddressTextBox is server control so the ID might be changed. AddressTextBox是服务器控件,因此可能会更改ID。 Try as: 尝试为:

var dst = document.getElementById("<%=AddressTextBox.ClientID%>").value;

This may resolve your error. 这样可以解决您的错误。

Just run this working piece of code, this might help you: 只需运行这段代码,这可能对您有帮助:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script type="text/javascript">
        function setValues()
        {
            document.getElementById('dest').value=document.getElementById('src').value;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="src" runat="server"></asp:TextBox>
            <asp:TextBox ID="dest" runat="server"></asp:TextBox>
            <input type="button" value="Set Value" onclick="setValues()" />
        </div>
    </form>
</body>
</html>

Now you can customise the solution as per your requirement. 现在,您可以根据需要自定义解决方案。

It looks that I made it work. 看来我已经做到了。 I just added 我刚刚添加

document.getElementById('ctl00_maincontent_FormView1_AddressTextBox').value = document.getElementById('autocomplete').value; document.getElementById('ctl00_maincontent_FormView1_AddressTextBox')。value = document.getElementById('autocomplete')。value;

into function fillInAddress() 进入功能fillInAddress()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM