【问题标题】:How to exchange value between an input and and asp:textbox controler如何在输入和 asp:textbox 控件之间交换值
【发布时间】:2015-03-01 10:02:27
【问题描述】:

如何使用这样的东西:

<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。 我怎么了?

【问题讨论】:

  • 我认为您的问题应该重命名,而不是“在代码后面获取 DOM 元素的值”应该类似于“如何在输入和 asp:textbox 控制器之间交换值”
  • 请尝试格式化并理解您的问题。

标签: javascript asp.net


【解决方案1】:

我认为你做错了。您创建一个元素输入,然后将您的文本框添加到其中,然后尝试获取元素输入的值 (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>

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

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

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

【讨论】:

  • 我认为信息窗口有问题 tmp.appendChild(document.getElementById(""));因为当我使用这条线时,信息窗口没有打开
  • @aliiii 您尝试在文本框中添加属性 ClientIDMode="Static" 并使用 tmp.appendChild(document.getElementById("txt_place_name")); ?无论如何,你尝试了我的第一个选项?
  • 我做到了!这一行没问题: tmp.appendChild(document.getElementById("txt_newpl"));但这条线不起作用:tmp.appendChild(document.getElementById("txt_place_name"));
  • 对不起 :-) 第二种方法是正确的,我的代码有一些额外的代码破坏了这个!!!非常感谢 adricadar
猜你喜欢
  • 1970-01-01
  • 2015-08-18
  • 1970-01-01
  • 2011-12-06
  • 1970-01-01
  • 2019-08-29
  • 2012-02-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多