最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript to add 2 numbers in textboxes in asp.net, doesnt work - Stack Overflow

programmeradmin0浏览0评论

this is my javascript to get addition of 2 values from 2 textboxes into the third....

its not working...

<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>

in textboxes

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

check my answer here

this is my javascript to get addition of 2 values from 2 textboxes into the third....

its not working...

<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>

in textboxes

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

check my answer here https://stackoverflow./a/11624756/1445836

Share Improve this question edited May 23, 2017 at 10:34 CommunityBot 11 silver badge asked Jul 23, 2012 at 12:18 ZoyaZoya 4053 gold badges10 silver badges22 bronze badges 5
  • 2 Is the capital "T" in "TxtAmount" a typo? (You have lower-case "t" in the other fields.) If you View Source in the browser are those ids the ones actually produced from the ASP code? – nnnnnn Commented Jul 23, 2012 at 12:21
  • could you write an else part and put an alert / console.log to find if any of those textboxes are null? – codeandcloud Commented Jul 23, 2012 at 12:26
  • Your code works for me: jsfiddle/r3SvQ so I assume what I mentioned in the previous ment is the problem. Try adding a ClientIDMode="Static" attribute to your inputs... – nnnnnn Commented Jul 23, 2012 at 12:38
  • See @hasanchuck's answer - you are using server side controls and their IDs may not be what you expect. – EdSF Commented Jul 23, 2012 at 13:45
  • got my answer stackoverflow./a/11624756/1445836 – Zoya Commented Jul 24, 2012 at 5:39
Add a ment  | 

3 Answers 3

Reset to default 3

Correct: if (c1 != null && c2 != null & c3 != null)

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

To menters: Sorry I never heard of Number(), since I usually don't work with front-end (especially math in front-end), mostly PHP/C#.

Also, c2, c3 are <input>'s ?

Maybe You can't get textboxes. Have you tried to give them clientidmode static?

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

here is the java script which worked for me

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;
                }

into the textboxes in .aspx page itself

<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>
发布评论

评论列表(0)

  1. 暂无评论