jquery: Call function by name on select change

后端 未结 6 2191
感动是毒
感动是毒 2021-02-20 01:12

I\'m trying to call a function by name .onchange of a select but nothing happens. When the function is describled after the attribute, it works.

THI

6条回答
  •  孤城傲影
    2021-02-20 01:32

    It is because the handler testMessage is not defined when binding it to the change event.

    It should work if it was in the same script context like below,

    
    

    Code inside are executed one by one progressive from top and testMessage function doesn't exist inside the first .

    You have couple of options here,

    1. Put it inside an anonymous function which will let your script to resolve the testMessage function later. [As suggested in Optimus Prime answer]

      
      
      
    2. Include the script that has testMessage function above the script that binds the testMessage like below,

      
      
      

提交回复
热议问题