Avoid modal dismiss on enter keypress

后端 未结 7 1843
暗喜
暗喜 2020-12-02 12:30

I have set up a bootstrap modal with a form inside it, I just noticed that when I press the Enter key, the modal gets dismissed. Is there a way not to dismiss it when pressi

7条回答
  •  一个人的身影
    2020-12-02 13:24

    I had this problem even after removing ALL buttons from my Bootstrap Modal, so none of the solutions here helped me.

    I found that a form with a single text field would cause the browser to do a form submit (and result in dismiss), if you hit Enter while keyboard focus is on the text field. This seems to be more of a browser/form issue than anything with Bootstrap.

    My solution was to set the form's onsubmit attribute to onsubmit="return false"

    This may be a problem if you are actually using the submit event, but I'm using JS frameworks that generate AJAX requests rather than doing a browser submit, so I prefer disabling submit entirely. (It also means I don't have to manually tweak every form element that might trigger a submit).

    More info here: Bootstrap modal dialogs with a single text input field always dismiss on Enter key

提交回复
热议问题