Using an HTML button to call a JavaScript function

前端 未结 10 1497
我在风中等你
我在风中等你 2020-11-22 09:59

I am trying to use an HTML button to call a JavaScript function.

Here\'s the code:



        
相关标签:
10条回答
  • 2020-11-22 10:28

    silly way:

    onclick="javascript:CapacityChart();"
    

    You should read about discrete javascript, and use a frameworks bind method to bind callbacks to dom events.

    0 讨论(0)
  • 2020-11-22 10:29

    I would say it would be better to add the javascript in an un-obtrusive manner...

    if using jQuery you could do something like:

    <script>
      $(document).ready(function(){
        $('#MyButton').click(function(){
           CapacityChart();
        });
      });
    </script>
    
    <input type="button" value="Capacity Chart" id="MyButton" >
    
    0 讨论(0)
  • 2020-11-22 10:29

    Just so you know, the semicolon(;) is not supposed to be there in the button when you call the function.

    So it should just look like this: onclick="CapacityChart()"

    then it all should work :)

    0 讨论(0)
  • 2020-11-22 10:33

    Your HTML and the way you call the function from the button look correct.

    The problem appears to be in the CapacityCount function. I'm getting this error in my console on Firefox 3.5: "document.all is undefined" on line 759 of bendelcorp.js.

    Edit:

    Looks like document.all is an IE-only thing and is a nonstandard way of accessing the DOM. If you use document.getElementById(), it should probably work. Example: document.getElementById("RUnits").value instead of document.all.Capacity.RUnits.value

    0 讨论(0)
提交回复
热议问题