2012-07-23 26 views
1

这是我的javascript获得另外2个文本框2个值的进入第三....JavaScript来在asp.net文本框添加2号,不工作

其不工作...

<script type="text/javascript"> 

    function calculate(ctrl1, ctrl2, ctrl3) { 

     var c1 = document.getElementById(ctrl1); 
     var c2 = document.getElementById(ctrl2); 
     var c3 = document.getElementById(ctrl3); 

     if (c1 != null && c2 != null & c3 != null) { 
      c3.value = Number(c1.value) + Number(c2.value); 

     } 
     document.forms[0].txteanum.focus(); 

    }  
</script> 
在文本框中

<asp:TextBox ID="txtQuantity" runat="server" onblur='javascript:calculate("txtQuantity","txtRate","TxtAmount")'></asp:TextBox> 

检查我的答案在这里https://stackoverflow.com/a/11624756/1445836

+2

是资本的 “T” 的Java脚本'一错字? (其他字段中的小写字母“t”。)如果浏览器中的查看源代码是那些实际从ASP代码生成的ID? – nnnnnn 2012-07-23 12:21:07

+0

什么是txteanum? – codingbiz 2012-07-23 12:24:58

+0

你能写一个else部分,并把一个alert/console.log来查找这些文本框中的任何一个是否为空? – naveen 2012-07-23 12:26:19

回答

0

这里是在` “TxtAmount” 工作对我来说

function Sum() { 
         var text1 = document.getElementById('<%= TextBox1.ClientID %>'); 
         var text2 = document.getElementById('<%= TextBox2.ClientID %>'); 
         if (text1.value.length == 0 || text2.value.length == 0) { 
           alert('Textbox1 and Textbox2 should not be empty'); 
           return; 
         } 

         var x = parseInt(text1.value); 
         var y = parseInt(text2.value); 
         document.getElementById('<%= TextBox3.ClientID %>').value = x +y; 
       } 

到文本框在.aspx页面中本身

<asp:TextBox ID="TextBox1" runat="server" onblur="Sum()"></asp:TextBox> 
<asp:TextBox ID="TextBox2" runat="server" onblur="Sum()"></asp:TextBox> 
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> 
3

正确:if (c1 != null && c2 != null & c3 != null)

TO:if (c1 != null && c2 != null && c3 != null)缺少&

要评议:对不起,我从来没有听说过号(),因为我通常不与前端(在前端尤其是数学)工作,主要是PHP/C# 。

另外,c2,c3<input>的?

+0

刷新你的知识年轻人) – micnic 2012-07-23 12:22:20

+1

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Number – naveen 2012-07-23 12:23:50

+1

永远愿意学习; )感谢您的参考。 – Novak 2012-07-23 12:25:26

2

也许你不能得到文本框。你有没有试图给他们clientidmode静态?

<asp:TextBox ID="txtQuantity" runat="server" onblur='javascript:calculate("txtQuantity","txtRate","TxtAmount")' ClientIDMode="static"></asp:TextBox> 
+0

对不起,仍然无法正常工作 – Zoya 2012-07-24 04:56:39

相关问题