Loading total on form load

2019-08-21 10:44发布

Hi I have following input which gets values and calls function fntotal to display total. The code works fine when values are changed. However when values are loaded in the form it does not display the total.

<input type="textssr" name="s{{=id}}" id="{{=id}}" value='{{=sAmount}}' 
onchange="fntotal(this.form)" />

  function fntotal(formObj) {
            var total = 0;
            total += parseInt(formObj.test1.value, 10)
            total += parseInt(formObj.test2.value, 10)
            total += parseInt(formObj.test3.value, 10)
            total += parseInt(formObj.s1020.value, 10)
            formObj.sum.value = total
        }

What I need to so that input displays the total on form load and onchange as well? Please help thanks

3条回答
来,给爷笑一个
2楼-- · 2019-08-21 11:18

Set an onLoad event listener to the <body> tag.

查看更多
一纸荒年 Trace。
3楼-- · 2019-08-21 11:20

I think following code will do, it is combination of jquery & javascript

$(document).ready(function(){
  var formObj = document.getElementById('<form_id>');
  fntotal(formObj);
});

You have to include jquery lib if you have not included it first.

To do that put following in begining of your tag.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
查看更多
戒情不戒烟
4楼-- · 2019-08-21 11:27

try writing your script code to calculate the sum right after the input tags like below

<input type="textssr" name="s{{=id}}" id="{{=id}}" value='{{=sAmount}}' 
 onchange="fntotal(this.form)" />
<script>

        var total = 0;
        total += parseInt(formObj.test1.value, 10)
        total += parseInt(formObj.test2.value, 10)
        total += parseInt(formObj.test3.value, 10)
        total += parseInt(formObj.s1020.value, 10)
        formObj.sum.value = total

</script>
查看更多
登录 后发表回答