Why is my onchange function called twice when using .focus()?

前端 未结 6 2059
执念已碎
执念已碎 2021-01-20 09:32

TLDR

  • Check this example in chrome.
  • Type someting and press tab. see one new box appear
  • type something and pr
6条回答
  •  一向
    一向 (楼主)
    2021-01-20 10:01

    The problem is indeed that because of the focus(), the onchange is called again. I don't know if this is a good sollution, but this adding this to the function is a quick sollution:

    context.onchange = "";
    

    (The onchange is called again, but is now empty. This is also good because this function should never be called twice. There will be some interface changes in the final product that help with problems that would arise from this (mistakes and all), but in the end this is something I probably would have done anyway).

    sollution here: http://jsfiddle.net/k4WKH/2/

    As @johnhunter says, the focus does not work in the example, but it does in my complete code. I haven't looked into what's going on there, but that seems to be a separate problem.

提交回复
热议问题