简体   繁体   English

自动计算TextBox的值

[英]Automatically calculate value of TextBox

How to get the value of textbox3 automatically by calculating textbox1-textbox2 when textbox1 and textbox2 values entered. 输入textbox1和textbox2值时,如何通过计算textbox1-textbox2自动获取textbox3的值。

<asp:TextBox ID="txtbox1" runat="server"></asp:TextBox>//enter value as 100
<asp:TextBox ID="txtbox2" runat="server"></asp:TextBox>//enter value as 50, Once we enter 50 result should appear in textbox3

<asp:TextBox ID="txtbox3" runat="server"></asp:TextBox>//Once we enter 50 result should appear in textbox3

txtbox3.Text = (Convert.ToInt32(txtbox1.Text) - Convert.ToInt32(txtbox2.Text)).ToString();

您需要在txtbox1和txtbox2上使用事件“ TextChanged”来进行计算

If is not necessary to call back the server side for this simple operation. 如果不需要,则可以通过简单的操作回叫服务器端。 you can try this : 你可以试试这个:

On Text1 and Text2 place onchange Event handler client side. 在Text1和Text2上,放置onchange事件处理程序客户端。

<asp:TextBox ID="txtbox1" runat="server" onchange='return calculateValueText3();'></asp:TextBox>

<asp:TextBox ID="txtbox2" runat="server" onchange='return calculateValueText3();'></asp:TextBox>

Add a Javascript section 添加一个Javascript部分

<script>
function calculateValueText3 ()
{
//for example
document.getElementById('<%=txtbox3.ClientID%>').value = 
document.getElementById('<%=txtbox1.ClientID%>').value - document.getElementById('<%=txtbox2.ClientID%>').value
}
</script>
<asp:TextBox ID="txt1" runat="server" onchange='return Calculate();'></asp:TextBox>

<asp:TextBox ID="txt2" runat="server" onchange='return Calculate();'></asp:TextBox>

if you are using javascript------ 如果您使用的是JavaScript ------

<script type="text/javascript">
function Calculate(){

document.getElementById('<%=txt3.ClientID%>').value = 
document.getElementById('<%=txt1.ClientID%>').value - document.getElementById('<%=txt2.ClientID%>').value;
}
</script>

now if you are using jquery------ 现在,如果您使用的是jquery--

function Calculate(){
$("#<%=txt3.ClientID%>").val() = 
$("#<%=txt1.ClientID%>").val - $("#<%=txt2.ClientID%>").val();
}

please change the IDs with your IDs this will work 请使用您的ID更改ID,这将起作用

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

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