• <legend id='GN3u2'><style id='GN3u2'><dir id='GN3u2'><q id='GN3u2'></q></dir></style></legend>

        <bdo id='GN3u2'></bdo><ul id='GN3u2'></ul>
      1. <small id='GN3u2'></small><noframes id='GN3u2'>

        <tfoot id='GN3u2'></tfoot>
        <i id='GN3u2'><tr id='GN3u2'><dt id='GN3u2'><q id='GN3u2'><span id='GN3u2'><b id='GN3u2'><form id='GN3u2'><ins id='GN3u2'></ins><ul id='GN3u2'></ul><sub id='GN3u2'></sub></form><legend id='GN3u2'></legend><bdo id='GN3u2'><pre id='GN3u2'><center id='GN3u2'></center></pre></bdo></b><th id='GN3u2'></th></span></q></dt></tr></i><div id='GN3u2'><tfoot id='GN3u2'></tfoot><dl id='GN3u2'><fieldset id='GN3u2'></fieldset></dl></div>
      2. 如何将输入标签中的两个数字相加?

        how to sum two numbers from input tag?(如何将输入标签中的两个数字相加?)
            <bdo id='ZoGZK'></bdo><ul id='ZoGZK'></ul>
            • <i id='ZoGZK'><tr id='ZoGZK'><dt id='ZoGZK'><q id='ZoGZK'><span id='ZoGZK'><b id='ZoGZK'><form id='ZoGZK'><ins id='ZoGZK'></ins><ul id='ZoGZK'></ul><sub id='ZoGZK'></sub></form><legend id='ZoGZK'></legend><bdo id='ZoGZK'><pre id='ZoGZK'><center id='ZoGZK'></center></pre></bdo></b><th id='ZoGZK'></th></span></q></dt></tr></i><div id='ZoGZK'><tfoot id='ZoGZK'></tfoot><dl id='ZoGZK'><fieldset id='ZoGZK'></fieldset></dl></div>

              <small id='ZoGZK'></small><noframes id='ZoGZK'>

                <tbody id='ZoGZK'></tbody>

            • <tfoot id='ZoGZK'></tfoot>
                • <legend id='ZoGZK'><style id='ZoGZK'><dir id='ZoGZK'><q id='ZoGZK'></q></dir></style></legend>

                  本文介绍了如何将输入标签中的两个数字相加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  可能重复:
                  如何将两个字符串像数字一样相加?

                  我写了一个简单的 JavaScript 代码,我想使用两个输入框并从两个值中添加数字.这是代码,我看到的结果是 1520 而不是 35.

                  I wrote a simple JavaScript code and I want to use two input box and add numbers from the two value. Here's the code, and I see the result 1520 instead of 35.

                  我该如何解决?

                  n1 <input type = "number" id = "n1" value=15 />
                  n2 <input type = "number" id = "n2" value=20 />
                  
                  <p>Sum?</p>
                  <button onclick="sum()">Try it</button>
                  <p id="demo2">Result?? </p>
                  
                  <script type="text/javascript">
                   function sum() 
                  { 
                      var fn, ln; 
                      fn = document.getElementById("n1").value; 
                      ln = document.getElementById("n2").value; 
                      result =  (fn+ln); 
                      document.getElementById("demo2").innerHTML = result; 
                  }
                  </script>
                  

                  推荐答案

                  使用 parseInt()parseFloat();您遇到的问题是您连接两个字符串,而不是添加两个数字.parseInt()(假设它找到一个实数)通过将字符串转换为数字来解决该问题:

                  Use parseInt(), or parseFloat(); the problem you were experiencing is that you were concatenating two strings, not adding two numbers. parseInt() (assuming that it finds a real number) addresses that issue by converting the string to a number:

                   function sum() 
                  { 
                      var fn, ln, result; 
                      fn = parseInt(document.getElementById("n1").value, 10);
                      ln = parseInt(document.getElementById("n2").value, 10);
                      result =  (fn+ln); 
                      document.getElementById("demo2").innerHTML = result; 
                  }
                  

                  出现在value之后的, 10是基数,它确保返回的数字(如果有的话)是哪个基数.

                  The , 10 that appears after the value is the radix, which ensures which number-base the returned number (if any) will be.

                  还要注意,result 变量也应该在函数中声明,以避免污染全局范围(并可能在其他地方与其他变量产生问题).

                  Also note that the result variable should be declared within the function as well, to avoid polluting the global scope (and possibly creating problems with other variables elsewhere).

                  参考资料:

                  • parseFloat().
                  • parseInt().

                  这篇关于如何将输入标签中的两个数字相加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  quot;Status Code:200 OK (from ServiceWorker)quot; in Chrome Network DevTools?(“状态码:200 OK(来自 ServiceWorker)在 Chrome 网络开发工具中?)
                  How to set a header for a HTTP GET request, and trigger file download?(如何为 HTTP GET 请求设置标头并触发文件下载?)
                  Adding custom HTTP headers using JavaScript(使用 JavaScript 添加自定义 HTTP 标头)
                  SQL Query DocumentDB in Azure Functions by an integer not working(通过整数在 Azure Functions 中 SQL 查询 DocumentDB 不起作用)
                  Azure Functions [JavaScript / Node.js] - HTTP call, good practices(Azure Functions [JavaScript/Node.js] - HTTP 调用,良好实践)
                  Azure Functions - Import Custom Node Module(Azure Functions - 导入自定义节点模块)
                  • <bdo id='ieuuF'></bdo><ul id='ieuuF'></ul>
                    <i id='ieuuF'><tr id='ieuuF'><dt id='ieuuF'><q id='ieuuF'><span id='ieuuF'><b id='ieuuF'><form id='ieuuF'><ins id='ieuuF'></ins><ul id='ieuuF'></ul><sub id='ieuuF'></sub></form><legend id='ieuuF'></legend><bdo id='ieuuF'><pre id='ieuuF'><center id='ieuuF'></center></pre></bdo></b><th id='ieuuF'></th></span></q></dt></tr></i><div id='ieuuF'><tfoot id='ieuuF'></tfoot><dl id='ieuuF'><fieldset id='ieuuF'></fieldset></dl></div>
                      <tbody id='ieuuF'></tbody>

                    <legend id='ieuuF'><style id='ieuuF'><dir id='ieuuF'><q id='ieuuF'></q></dir></style></legend>
                      • <small id='ieuuF'></small><noframes id='ieuuF'>

                        1. <tfoot id='ieuuF'></tfoot>