简体   繁体   中英

JavaScript How to pass a parameter to a function

I am having trouble passing a parameter to a function.

Here is my code (edited to show flow and positioning in the html doc):

<!-- this src file precedes -->
    // this resides in a js src file on its own and as is
    function mySpecialFunction(thisIndex) {
      alert(thisIndex); // shows as blank
    }

<!-- this src file follows -->
$(document).ready(function() {
  $(window).load(function() {
    $(document).on('change', '.jq__pAC', function(event) {
    var i = 1;
    // some JavaScript code
    mySpecialFunction(i);
    // some more JavaScript code
    }); // end jq__pAC
  }); // end .load()
}); // end .ready()

my alert() event displays blank.

The 2 blocks of js code are in separate src files. Does that have anything to do with it?

If you say the functions are in different files, probably the order of the files in the problem.

Make sure that the calling method ( mySpecialFunction(i); ) comes after the declaration ( function mySpecialFunction(thisIndex) ).

EDIT:

In your edit you have $(document).ready and $(window).load . Remove the last one and it will work.

See jsfiddle .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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