如何使用javascript将文本框值应用于标签? [英] how to apply text box value to label using javascript?

查看:99
本文介绍了如何使用javascript将文本框值应用于标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框和一个标签..当用户在文本框中写入任何内容时,我需要在文本框中立即更新文本..如何使用javascript实现这一点?这是我的代码。它不能正常工作



< asp:TextBox id =txtbox1runat =serveronchange =settext()>

 <   asp:标签    ID   =  Label6 < span class =code-attribute>   runat   =  server  ForeColor   = 白色   宽度  =  200px >  <   / asp:Label  < span class =code-keyword>>  





 <   script  >  
function settext( )
{
var name = document.getElementById('<% = txtbox1.ClientID % > ')值;
document.getElementById(Label6)。innerHTML = name;
}
< / script >

解决方案

试试这个:

 <   script  >  
function settext(e){
document.getElementById(<% = Label1.ClientID %> )。innerHTML = e.value;
}
< / script >
< / head >
< body >
< 表格 id = form1 runat = server >
< div >
< asp:TextBox ID = TextBox1 runat = server onkeyup = settext(this); onkeydown = settext(this); > < / asp:TextBox >
< asp:Label ID = Label1 runat = < span class =code-keyword> server 文本 = 标签 > dd < / asp:标签 >
< / div >
< span class =code-keyword>< / form >
< / body >


有两种方式::



第一个是:



 <   asp:label     id   =  Label6     runat   =  server    forecolor   = 白色    width   =  200px    xmlns:asp   = #unknown >  <   / asp:label  < span class =code-keyword>>  





< script> 
function settext()
{
var name = document .getElementById(' <%= txtbox1.ClientID%> ;)值;
document .getElementById(' <% = Label6.ClientID%>')。innerHTML = name;
}
< / script>







第二个是





 <   asp:label     id   =  Label6    runat   =  server   < span class =code-attribute> forecolor   = 白色    width   =  200px    ClientIdMode   = 静态    xmlns:asp   = #unknown >  <   / asp:label  >  





< script> 
function settext()
{
var name = document .getElementById(' & lt;%= txtbox1.ClientID %安培; GT;')值。
document .getElementById(' Label6').innerHTML = name;
}
< / script>


尝试文本框的onkeypress事件.. :)



  function  settext(valueData)
{

document .getElementById(' <%= Label6.ClientID%> ')。innerHTML = valueData;
}





HEML



 <   asp:textbox     id   =  txtbox1     runat   = < span class =code-keyword> server    onkeypress   =  settext(this.value)   >  
< / asp:textbox < span class =code-keyword>>


i have a textbox and a label..when user writes anything in textbox i need the text in textbox to be immediately updated in label..how to achieve this using javascript?this is my code.and it is not working

<asp:TextBox id="txtbox1" runat="server" onchange="settext()">

<asp:Label ID="Label6" runat="server"ForeColor="White" Width="200px"></asp:Label>



<script>
 function settext()
{
    var name = document.getElementById('<%= txtbox1.ClientID %>').value;
    document.getElementById("Label6").innerHTML = name;
}
</script>

解决方案

Try this:

<script>
        function settext(e) {
            document.getElementById("<%= Label1.ClientID %>").innerHTML = e.value;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" onkeyup="settext(this);" onkeydown="settext(this);"></asp:TextBox>
        <asp:Label ID="Label1" runat="server" Text="Label">dd</asp:Label>
    </div>
    </form>
</body>


There are two ways for it::

1st one is:

<asp:label id="Label6" runat="server" forecolor="White" width="200px" xmlns:asp="#unknown"></asp:label>



<script>
 function settext()
{
    var name = document.getElementById('<%= txtbox1.ClientID %>').value;
    document.getElementById('<%=Label6.ClientID%>').innerHTML = name;
}
</script>




2nd one is


<asp:label id="Label6" runat="server" forecolor="White" width="200px" ClientIdMode="Static" xmlns:asp="#unknown"></asp:label>



<script>
function settext()
{
    var name = document.getElementById('&lt;%= txtbox1.ClientID %&gt;').value;
    document.getElementById('Label6').innerHTML = name;
}
</script>


try onkeypress event of textbox.. :)

function settext(valueData)
{
  
    document.getElementById('<%=Label6.ClientID%>').innerHTML = valueData;
}



HEML

<asp:textbox id="txtbox1" runat="server" onkeypress="settext(this.value)" >
</asp:textbox>


这篇关于如何使用javascript将文本框值应用于标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆