现在有三个文本框,第一个为价格,第二个为数量,第三个为总价,我想在第一、二中输入价格和数量后在第三个文本框中自动出现总价,请问应该怎么写这段代码,应该用哪个事件,还请多多指教,谢谢,急用!!!

解决方案 »

  1.   

    本帖最后由 net_lover 于 2010-11-10 13:46:19 编辑
      

  2.   

    可以定义第二个文本框的TextChanged事件,将其自动计算
      

  3.   

    把前两个文本框的autopostback设为true再分别写它们的文本框的TextChanged事件在事件里判断另一个文本框是否为空当两个文本框都不为空时。。给第三个文本框赋值两个TextChanged事件中代码都一样
      

  4.   


    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script>
          function GetAll() {
            var t1 = parseFloat(document.getElementById("<%=TextBox1.ClientID %>").value);
            var t2 = parseFloat(document.getElementById("<%=TextBox2.ClientID %>").value);
            t1 = isNaN(t1) ? 0 : t1;
            t2 = isNaN(t2) ? 0 : t2;
            document.getElementById("<%=TextBox3.ClientID %>").value = t1 * t2;
          }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">  
        <asp:TextBox ID="TextBox1" runat="server" onblur="GetAll()"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server" onblur="GetAll()"></asp:TextBox>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>  
        </form>
    </body>
    </html>
      

  5.   

    前台 <head runat="server">
        <title></title>
        <script type="text/javascript">
            function getPrice(a, b) {            document.getElementById("TextBox3").value = document.getElementById(a).value * document.getElementById(b).value;
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        </form>
    </body>
    后台protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.TextBox1.Attributes.Add("onpropertychange", "getPrice('TextBox1','TextBox2');");
                this.TextBox2.Attributes.Add("onpropertychange", "getPrice('TextBox1','TextBox2');");
            }
        }