问题描述
海亲爱的朋友,
我在网页上有3个文本框...如果我在两个文本中输入值,则自动将第三个文本框shd获取这两个文本框值的相减值...你能帮我吗?????????
hai dear friends,
i hav 3 textbox on my webpage...if i enter value in two text ,then automatically the third textbox shd get the subtracted value of these two textbox value...Can u help me?????????
推荐答案
<asp:Textbox id="textbox2" runat="server" onBlur = "Javascript:SubstactData();" />
<script>
function SubstactData()
{
var txt1 = document.getElementById('textbox1');
var txt2 = document.getElementById('textbox2');
var txt3 = document.getElementById('textbox3');
txt3.value = parseInt(txt1.value) - parseInt(txt2.value);
}
</script>
如果您解决了问题,请标记为答案
Mark as answer if solved your problem
var TestCal = {
firstTextBoxID: "",
secondTextBoxID: "",
calTextBoxID: "",
Initialize: function (submittedFirstTextBoxID, submittedSecondTextBoxID, submittedCalTextBox) {
this.firstTextBoxID = submittedFirstTextBoxID;
this.secondTextBoxID = submittedSecondTextBoxID;
this.calTextBoxID = submittedCalTextBox;
},
Calculation: function () {
var firstTextBoxValue = document.getElementById(this.firstTextBoxID).value;
var secondTextBoxValue = document.getElementById(this.secondTextBoxID).value;
var firstValue = parseInt(firstTextBoxValue != "" && firstTextBoxValue != "undefind" ? firstTextBoxValue : "0");
var secondValue = parseInt(secondTextBoxValue != "" && secondTextBoxValue != "undefind" ? secondTextBoxValue : "0");
var CalculatedValue = firstValue - secondValue;
document.getElementById(this.calTextBoxID).value = CalculatedValue.toString();
}
}
aspx文件如下所示,
The aspx file is given below,
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:TextBox runat="server" ID="firstNumber" >
<asp:TextBox runat="server" ID="secondNumber">
<asp:TextBox runat="server" ID="calculatedNumber" >
.cs文件如下所示,
And the .cs file is given below,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(), "TestCall"))
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "TestCall",
"TestCal.Initialize('" + this.firstNumber.ClientID + "','" + this.secondNumber.ClientID + "','" + this.calculatedNumber.ClientID + "');", true);
}
string jsCalculation = "TestCal.Calculation()";
firstNumber.Attributes.Add("OnKeyUp", jsCalculation);
secondNumber.Attributes.Add("OnKeyUp", jsCalculation);
}
}
好吧,现在运行此命令并将值放在第一个两个文本框中,第三个将显示结果.在这里,我没有考虑任何验证.您需要以自己的方式进行验证.
Okay now run this and put value to fist two text boxes and the third one will display you the result. Here i didn''t consider any validation. You need to do the validation in your own way.
txtTwo.Attributes.Add("onchange", "Calculate();");
并将javascript添加到ur aspx页面
and add javascript to ur aspx page
<script type="text/javascript">
function Calculate() {
var txtOne = document.getElementById('txtValue1');
var txtTwo = document.getElementById('txtValue2');
var txtAns = document.getElementById('txtValueAns');
txtAns.value=parseFloat(txtOne.value)-parseFloat(txtTwo.value);
}
</script>
这篇关于文本框或Javascript的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!