0

I have a JavaScript event which calculates a price of something according to 2 variables (price and days that the product is used). The function is called on the 2 input elements like this: onkeyup=calc(this.form), this works like a charm.

I've connected a DB with these 2 values and echo-ed them as values of the two input elements (which is successful) and added this to make the function happen when the page is loaded:

<script type="text/javascript">
    window.onload = function()
         {
             calc(form);
         }; 
</script>

The problem is that the result of the calculation doesn't show up until I do something with one of the input elements.

The live version can be seen over here: http://www.websane.nl/macbook/opgeslagen.php?id=1

Do you know what the problem is?

4

1 回答 1

2

The problem is that the form in the statement calc(form); isn't anything--it's undefined. Which makes sense--not only can there be many forms in an HTML document (so it could never automatically work), you never bound the form variable to any one of them. To get a reference to a particular form, you can use several techniques:

  • document.getElementsbyTagName('form')[0]
  • document.forms[0]
  • document.forms.formname
  • document.getElementById('theFormId')

Replace form in your onload event with one of the above and things should work. There could be many other ways to get a reference to a form.

于 2013-03-31T02:35:50.110 回答