Skip to content

Instantly share code, notes, and snippets.

@niyari
Created November 16, 2018 07:03
Show Gist options
  • Save niyari/eba9cb91de6b89fcf2af4b89f85cc0ea to your computer and use it in GitHub Desktop.
Save niyari/eba9cb91de6b89fcf2af4b89f85cc0ea to your computer and use it in GitHub Desktop.
jQueryを使わず雑にINPUTでEnter押しても送信しないやつ
// input の class に allow_submit が含まれていると Enter で送信できるようになります。
// classList を使っているのでIE10未満は動作しません。jQueryを使うと良いのではないでしょうか。
// TypeScript
(() => {
let isEnter = false;
document.addEventListener("keypress", (event) => {
let target = <Node>event.target;
if (event.key === "Enter" && target.nodeName === "INPUT") {
let target = <HTMLInputElement>event.target;
if (target.classList.contains("allow_submit") === false) {
isEnter = true;
}
}
}, false);
document.addEventListener("submit", (event) => {
if (isEnter) {
event.preventDefault();
}
isEnter = false;
}, false);
})();
//JavaScript に変換すると、こうなるようです。
(function () {
var isEnter = false;
document.addEventListener("keypress", function (event) {
var target = event.target;
if (event.key === "Enter" && target.nodeName === "INPUT") {
var target_1 = event.target;
if (target_1.classList.contains("allow_submit") === false) {
isEnter = true;
}
}
}, false);
document.addEventListener("submit", function (event) {
if (isEnter) {
event.preventDefault();
}
isEnter = false;
}, false);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment