Uncaught TypeError: object is not a function, butt

2019-05-25 05:37发布

I have the following snippet of code in my jsp page:

<form name="programarRutasForm" method="post" action="/SGT_Galp_web/programarRutas.do">   
     <table>
        <tr>
          <td>
            <input type="button" name="insereVuelta" value="Insere Vuelta" onclick="insereVuelta()" id="insereVuelta">
          </td>
          <td>
            <input type="submit" name="criaRuta" value="Cria Ruta" id="criaRuta">
          </td>
        </tr>
     </table>
     ...

When I click the button insereVuelta, it throws an exception with the message:

Uncaught TypeError: object is not a function

But if I put the button outside the form it works normally. How is this happening? I really need to put the button near the submit button inside the form.

3条回答
我欲成王,谁敢阻挡
2楼-- · 2019-05-25 05:59

The Error is because of problems with Semi-colons. Check if your script has lines that are missing semi-colons. You would ask "then why does it work outside the form?" The Automatic-Semicolon insertion fails in certain situations; you can read about it more here: What are the rules for JavaScript's automatic semicolon insertion (ASI)?

I feel this would solve the problem.

查看更多
别忘想泡老子
3楼-- · 2019-05-25 06:03

I believe the problem stems from the function having the same name as the button itself. I had the same problem and changing the name of the button fixed it. It looks like changing the name of your button fixed it for you, as well.

Conjecture:

I would guess that when the names are the same, the interpreter is trying to call the button itself, rather than the function. Because the button isn't a function, it throws the error.

I don't have an explanation for why it worked for you when outside the form, though. Perhaps when a button is outside the form, the interpreter doesn't automatically wire it up as an accessible object in the same way, or maybe it would have to be accessed via a "parent".

查看更多
Root(大扎)
4楼-- · 2019-05-25 06:09

insereVuelta is used as the name and id of the input and the name of the function, try changing name and id or the function name.

查看更多
登录 后发表回答