如何在输入和asp:textbox控件之间交换值

How to exchange value between an input and and asp:textbox controler

本文关键字:控件 textbox 之间 交换 asp 输入      更新时间:2024-07-05

如何使用这样的东西:

<asp:TextBox ID="txt_place_name" CssClass="form-control ltr"  ValidationGroup="add" runat="server"></asp:TextBox>
    <input type="text" id="txt_newpl" value="placeName" runat="server" />

javascript代码为:

    <script>
var tmp = document.createElement("input");
tmp.appendChild(document.getElementById('txt_newpl'));
google.maps.event.addListener(marker, "click", function (e) {
    var infoWindow = new google.maps.InfoWindow({
       content: 
       '<div>'+tmp.innerHTML+'</div>'
    })
});
document.getElementById("<%= txt_place_name.ClientID%>").value = txt_java.value;
</script>

我想将txt_newpl的值传输到txt_place_name。我怎么了?

我认为你做错了。您创建了一个元素输入,然后将您的文本框附加到它上,然后您试图获得元素输入txt_java.value)的值,这是错误的,您想要文本框值(document.getElementById('txt_newpl').value)。

<script>
    // .. rest of the code ...
    document.getElementById("<%= txt_place_name.ClientID%>").value = document.getElementById('txt_newpl').value;
</script>

此外,如果您将<asp:TextBox ... />添加到要完成的信息窗口中,也不容易,就像这样。这样就不会因为不必要的值交换而使代码复杂化。

<asp:TextBox ID="txt_place_name" CssClass="form-control ltr" ValidationGroup="add" runat="server"></asp:TextBox>
<script>
    var tmp = document.createElement("input");
    tmp.appendChild(document.getElementById("<%= txt_place_name.ClientID %>"));
    google.maps.event.addListener(marker, "click", function (e) {
        var infoWindow = new google.maps.InfoWindow({
            content:
            '<div>' + tmp.innerHTML + '</div>'
        })
    });
</script>

您可以这样做,因为<asp:TextBox .../>控件将呈现为html元素<input type="text" ... />。此渲染(转换)过程发生在服务器端上。