简体   繁体   English

在onload事件中使用Javascript添加两个数字

[英]Add two numbers using Javascript on onload event

How can i add two numbers from text box when the page loads? 页面加载后,如何从文本框中添加两个数字? it only works when i try to create a button for adding. 它仅在我尝试创建要添加的按钮时有效。

 function sum() { var txtFirstNumberValue = document.getElementById('totalprice').value; var txtSecondNumberValue = document.getElementById('price1').value; var result = parseInt(txtFirstNumberValue) * parseInt(txtSecondNumberValue); if (!isNaN(result)) { document.getElementById('totalprices').value = result; } } 

use window.onload 使用window.onload

<script>
  function sum() {
            var txtFirstNumberValue = document.getElementById('totalprice').value;
            var txtSecondNumberValue = document.getElementById('price1').value;
            var result = parseInt(txtFirstNumberValue) * parseInt(txtSecondNumberValue);
            if (!isNaN(result)) {
                document.getElementById('totalprices').value = result;
            }
        }
  window.onload=sum;
</script>

You either need to call the function when your document has finished loading with the window.onload listener https://developer.mozilla.org/en/docs/Web/API/GlobalEventHandlers/onload 当您使用window.onload侦听器完成文档加载后,您要么需要调用该函数window.onload侦听器https://developer.mozilla.org/en/docs/Web/API/GlobalEventHandlers/onload

ie

function sum(){
     var txtFirstNumberValue = document.getElementById('totalprice').value;
            var txtSecondNumberValue = document.getElementById('price1').value;
            var result = parseInt(txtFirstNumberValue) * parseInt(txtSecondNumberValue);
            if (!isNaN(result)) {
                document.getElementById('totalprices').value = result;
            }
        }
window.onload = sum;

Alternatively, you can run the code after the HTML in the document and without it being in a function and then it will interpret immediately 或者,您可以在文档中的HTML之后运行代码,而无需将其包含在函数中,然后它将立即解释

eg 例如

<html>

<div>some stuff here</div>
... // bottom of document
<script>
var txtFirstNumberValue = document.getElementById('totalprice').value;
                var txtSecondNumberValue = document.getElementById('price1').value;
                var result = parseInt(txtFirstNumberValue) * parseInt(txtSecondNumberValue);
                if (!isNaN(result)) {
                    document.getElementById('totalprices').value = result;
                }
</script>
</html>

If you prefer jQuery over regular Javascript: 如果您更喜欢jQuery,而不是常规Javascript:

$(document).ready(function() {
    var txtFirstNumberValue = $('#totalprice').val();
    var txtSecondNumberValue = $('#price1').val();
    var result = (parseInt(txtFirstNumberValue) * parseInt(txtSecondNumberValue));
    if (!isNaN(result)) {
        $('#totalprices').val(result);
    }
});

This will also fire sooner than a window.onload , resulting in a better user experience. 这也会比window.onload更快地触发,从而带来更好的用户体验。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM