文本框或Javascript的事件 [英] Events of textbox or Javascript

查看:85
本文介绍了文本框或Javascript的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海亲爱的朋友,
我在网页上有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


好.这很容易.如果要从Js维护它,则需要传递其CLientID.我给你举个例子.

这是我的Js文件.我已将此文件包含在母版页"中.看看请

Okay . This is easy enough. If you want to maintain it from Js You need to pass its CLientID. I am giving you an example.

This is my Js file. I have included this file in the Master Page. Have a look Please

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.


将事件添加到文本框中
add event to your text box
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的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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