如何将文本从asp.net webfroms aspx中的asp:textbox复制到另一个asp:textbox

如何将文本从asp.net webfroms aspx中的asp:textbox复制到另一个asp:textbox,asp.net,webforms,textbox,copy,Asp.net,Webforms,Textbox,Copy,几个小时以来我一直在寻找解决办法 asp:文本框1: <asp:TextBox runat="server" ID="code" placeholder="<%$ Resources:ErrorMessages, EnterCode %>" CssClass="enterCode"></asp:TextBox><br /> 链接到aspx文件的javascript文件中的某些jquery: $("#buttonEnable2").click(f

几个小时以来我一直在寻找解决办法

asp:文本框1:

<asp:TextBox runat="server" ID="code" placeholder="<%$ Resources:ErrorMessages, EnterCode %>" CssClass="enterCode"></asp:TextBox><br /> 
链接到aspx文件的javascript文件中的某些jquery:

$("#buttonEnable2").click(function () {
        $('#code').val($('#code2').val());
        alert('test');

    });

上面的代码似乎根本不起作用,它甚至没有给我一个提示。

这段代码是用javascript编写的

  <script>
            function Copy() {
                var n1 = document.getElementById('code');
                var n2 = document.getElementById('code2');
                n2.value = n1.value;
            }
        </script>

函数副本(){
var n1=document.getElementById('code');
var n2=document.getElementById('code2');
n2.value=n1.value;
}
你的按钮应该是



希望它能有所帮助。

您的文本框是
asp.net
的,它改变了文本框的
ID
与其
内容占位符的关系。因此jquery无法找到文本框

您可以使用
clientdmode=“Static”
,这样文本框的ID就不会改变。将此属性也放在其他文本框中

<asp:TextBox runat="server" ID="code" ClientIDMode="Static" placeholder="<%$ Resources:ErrorMessages, EnterCode %>" CssClass="enterCode"></asp:TextBox>

如果您在asp.net中使用母版页、更新面板等,文本框的ID将自动替换为动态ID,因此您可能需要执行以下操作,如果您不想将ClientMode更改为静态,这将有助于

首先声明一个javascript变量,并将文本框的动态id分配给它,JQuery将为其

$(document).ready(function(){ 
  var txtBox1 = <%=code.ClientId%>;
  var txtBox2 = <%=code2.ClientId%>;
});
您还可以按以下方式使用document.getElementById

var n1 = document.getElementById(<%=code.ClientId%>);
var n2 = document.getElementById(<%=code2.ClientId%>);
var n1=document.getElementById();
var n2=document.getElementById();

我希望这会对你有所帮助。

问题是你在一个popover里面使用一个手风琴,它克隆了popover里面的所有东西,处理了所有东西的ID。因此,当您在文本框中键入值时,它会将其附加到文本框的克隆,而不是实际的文本框

以下是解决方案:

function clickEnable2() {
    var txtBox1 = this.parentElement.getElementsByClassName("enterCode")[0].value;
    $("#MainContent_code").val(txtBox1);
    $("#MainContent_buttonEnable").click();
}
$("#MainContent_buttonEnable2").click(clickEnable2);

如果您使用jquery@Vijay Gautam,请使用OnclientClick事件。我将如何做到这一点?
$('#code').val($('#code2').val());
$(document).ready(function(){ 
  var txtBox1 = <%=code.ClientId%>;
  var txtBox2 = <%=code2.ClientId%>;
});
$("#buttonEnable2").click(function () {
    $('#' + txtBox1).val($('#' + txtBox2).val());
});
var n1 = document.getElementById(<%=code.ClientId%>);
var n2 = document.getElementById(<%=code2.ClientId%>);
function clickEnable2() {
    var txtBox1 = this.parentElement.getElementsByClassName("enterCode")[0].value;
    $("#MainContent_code").val(txtBox1);
    $("#MainContent_buttonEnable").click();
}
$("#MainContent_buttonEnable2").click(clickEnable2);