简体   繁体   中英

How do I call a function through onChange with parameters

My javascript function sumCol(var mn, var t) is in the with two parameters that are both supposed to be strings.

I call the function from my HTML code. I have a textbox that calls it onchange:

<input type="text" size = 15 id = "Mhour" onchange="sumCol(\''+Mhour+'\', \''+Mtot+'\')">

Mhour and Mtot are the strings that I need passed into the function when I click out of the textbox.

The function works when i don't use parameters and have the string variables set into the code but I need to use the same function in multiple places and these variable change.

Thank you!

if your sumCol function expected two string parameters then simply pass two as like this

onchange="sumCol('Mhour', 'Mtot')

if you want to pass string variable into this then first declare them and then pass them here

   var param1="parameter1";
   var param2="parameter2";

  onchange="sumCol(param1,param2)"

Just noticed you have the following parameters for your sumCol function:

sumCol(var mn, var t) { //... };

should be

sumCol(mn, t) { //... };

The function does't actually take 2 proper variables but instead it takes 2 parameters or arguments, this might explain this better Functions

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