2012-07-23 34 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

相關問題