Hi! I don't know exactly why it does not allow jumping, but it is clear that the code is not correctly done for the nice behaviour. Some advices to improve and restructure your code (although I could be wrong in some of them):
1) I think that the "i" variable is not being used, so you can remove the lines 16 and 36.
2) You could write "local hero = self:get_game():get_hero()" to use the hero variable, avoiding to repeat the code "self:get_game():get_hero()". The same for the game variable.
3) The on_update() event is called very quickly, almost every instant, so it is not a good idea to change the position of hero when it is called. You could use also some timer to change the position of the hero with the desired speed (use a falling speed variable for this timer). Be careful so that each time the event is called you create the timer only if there is not already some timer created before.
4) Changing the position of the hero with "hero:set_position(x,y+gravity,l)" is not a good idea. If the gravity is bigger than 1, say 8 for instance, then in the case that the hero has coordinate "y" 4 coordinates above the ground he would not fall because there is an obstacle 8 coordinates under him. (The hero should fall 1 pixel each time, and set the falling speed with a timer as I suggested above.)
I will try to help you more after you rewrite/improve your code.