Cara mencegah tombol Enter dari mengirimkan formulir sebenarnya sangat sederhana, hanya dalam satu kalimat. OnkeyDown = "if (event.keycode == 13) return false;" Cukup tulis kalimat ini di tag dari.
Jika Anda menekan ENTER pada halaman untuk mengirimkan secara otomatis, Anda dapat mengatur nilai pengembalian untuk mencegah pengiriman otomatis, seperti <input id = "q" name = "q" type = "text" onkeypress = "JavaScript: return gosearch ();"> Nilai pengembalian harus salah
Salinan kode adalah sebagai berikut:
fungsi gosearch ()
{
if (window.event.keycode == 13)
{
mencari();
mengembalikan false;
}
}
Atau tulis secara langsung dalam bentuk
Salinan kode adalah sebagai berikut:
<Form name = "Form1" action = "action/soft_add_action.php" enctype = "multipart/form-data" metode = "POST" ONKEYDOWN = "if (event.keycode == 13) Return false;" onsubmit = "return checkSubmit ();">
Atau lihat kode berikut:
Salinan kode adalah sebagai berikut:
<Html>
<head>
<title> tekan enter untuk mengirimkan formulir </iteme>
</head>
<body>
<Form name = "myForm" action = "" onKeyDown = "if (event.keycode == 13) {return false;}">
<Input Tipe = Nama Teks = Pengguna>
<Input Tipe = Nama Teks = PASS>
<input type = name tombol = mybutton value = kirim onClick = myForm.submit ()>
</form>
Anda dapat menambahkan penilaian formulir dalam fungsi "myForm.submit ()", cukup ubah nama fungsi seperti penilaian bentuk umum.
</body>
</html>
Berikut ini adalah pengantar event.keycode di JavaScript.
Kode Key 8 = Backspace Backspace
Kode keycode 9 = tab tab
Kode Key 12 = Hapus
kode kunci 13 = masukkan
Kode keycode 16 = shift_l
Kode keycode 17 = control_l
Kode keycode 18 = alt_l
Kode keycode 19 = jeda
Kode keycode 20 = caps_lock
Kode Key 27 = Escape Escape
Kode Key 32 = Ruang Ruang
Kode Key 33 = Sebelumnya
Kode Key 34 = Berikutnya
Kode Key 35 = Akhir
Kode Key 36 = Beranda
Kode Key 37 = kiri
kode kunci 38 = up
Kode Key 39 = benar
Kode Key 40 = Down
Kode Key 41 = Pilih
Kode Key 42 = Cetak
Kode keycode 43 = Eksekusi
Kode keycode 45 = masukkan
Kode Key 46 = Hapus
Kode keycode 47 = BANTUAN
Kode keycode 48 = 0 Equal Braceright
Kode keycode 49 = 1 cerpam Onesuperior
Kode keycode 50 = 2 TwoSurerior QuotedBL
Kode Kunci 51 = 3 bagian Threesuperior
Kode keycode 52 = 4 dolar
Kode keycode 53 = 5 persen
Kode Key 54 = 6 Ampersand
Kode Key 55 = 7 Slash Braceleft
Kode keycode 56 = 8 Parentleft Brakeleft
Kode keycode 57 = 9 ParentRight Bracketright
Kode Key 65 = a
Kode Kunci 66 = B B
Kode Kunci 67 = C C
Kode Key 68 = D D
Kode Key 69 = E Eurosign
kode kunci 70 = f f
kode kunci 71 = g g
kode kunci 72 = h h
Kode keycode 73 = i i
Kode Key 74 = J J
kode kunci 75 = k k
kode kunci 76 = l l
Kode Kunci 77 = M M Mu
Kode Kunci 78 = N N
Kode Kunci 79 = O O
Kode Kunci 80 = P P
Kode Key 81 = Q Q at
kode kunci 82 = r r
kode kunci 83 = s
kode kunci 84 = t t
Kode Key 85 = U U
kode kunci 86 = v v
Kode keycode 87 = w w
Kode Key 88 = x x
Kode Key 89 = y y
Kode Key 90 = Z Z
Kode keycode 96 = kp_0 kp_0
Kode keycode 97 = kp_1 kp_1
Kode keycode 98 = kp_2 kp_2
Kode keycode 99 = kp_3 kp_3
Kode keycode 100 = kp_4 kp_4
KapeCode 101 = kp_5 kp_5
KAPE KEY 102 = KP_6 kp_6
Kode keycode 103 = kp_7 kp_7
Kode keycode 104 = kp_8 kp_8
Kode keycode 105 = kp_9 kp_9
Kunci Key 106 = kp_multiply kp_multiply
Kode keycode 107 = kp_add kp_add
Kode keycode 108 = kp_separator kp_separator
Kunci Key 109 = kp_subtract kp_subtract
Kode keycode 110 = kp_decimal kp_decimal
Kode Key 111 = kp_divide kp_divide
Kode Key 112 = F1
Kode Key 113 = F2
Kode Key 114 = F3
Kode Key 115 = F4
Kode Key 116 = F5
Kode Key 117 = F6
Kode Key 118 = F7
Kode Key 119 = F8
Kode Key 120 = F9
Kode Key 121 = F10
Kode Key 122 = F11
Kode Key 123 = F12
Kode Key 124 = F13
Kode Key 125 = F14
Kode Key 126 = F15
Kode Key 127 = F16
Kode Key 128 = F17
Kode Key 129 = F18
Kode Key 130 = F19
Kode Key 131 = F20
Kode Key 132 = F21
Kode Key 133 = F22
Kode Key 134 = F23
Kode Key 135 = F24
Kode keycode 136 = num_lock
Kode keycode 137 = scroll_lock
Kode Key 187 = Makam Akut
Kode keycode 188 = koma titik koma
Kode keycode 189 = minus garis bawah
Kode keycode 190 = periode usus besar
Kode keycode 192 = Apostrophe Numbersign
Kode keycode 210 = plusminus hyphen macron
Kode keycode 211 =
Kode Key 212 = Hak Cipta Terdaftar
Kode Key 213 = Guillemotleft Guillemoright
Kode keycode 214 = ordfeminin maskulin
Kode keycode 215 = AE AE
kode kunci 216 = sen yen
Kode keycode 217 = Iklamdown Tanya Jawab
Kode keycode 218 = Onequarter OneHalf Threequarters
Kode keycode 220 = bilah yang kurang lebih besar
Kode Key 221 = Plus Asterisk Asciticilde
Kode Key 227 = Divisi Gandakan
Kode keycode 228 = acircumflex acircumflex
Kode Key 229 = Ecircumflex Ecircumflex
Kode keycode 230 = icircumflex icircumflex
Kode keycode 231 = ocircumflex ocircumflex
Kode keycode 232 = UCIRCUMFLEX UCIRCUMFLEX
Kode keycode 233 = nilde ntilde
kode kunci 234 = yacute yacute
Kode keycode 235 = Oslash OoBlique
kode kunci 236 = aring aring
Kode keycode 237 = ccedilla ccedilla
Kode keycode 238 = Thorn Thorn
Kode Kunci 239 = ETH ETH
Kode Key 240 = Mata Uang Cedilla Diaeresis
Kode Key 241 = Agrave Agrave Atilde Atilde
Kode Key 242 = Egrave Egrave
Kode Key 243 = Igrave Igrave
Kode Key 244 = Ograve Ograve Otilde Otilde
Kode Key 245 = UGrave UGrave
Kode Key 246 = Adiaeresis Adiaeresis
Kode keycode 247 = Ediaeresis Ediaeresis
Kode Key 248 = Idiaeresis Idiaeresis
Kode Key 249 = Odiaeresis Odiaeresis
Kode keycode 250 = Udiaeresis udiaeresis
Kode keycode 251 = ssharp pertanyaan backslash
Kode keycode 252 = derajat asciicircum
kode kunci 253 = 3 sterling
Kode keycode 254 = Mode_switch
Gunakan properti kode kunci dari objek acara untuk menentukan nilai kunci input
misalnya: if (event.keycode == 13) peringatan ("Enter!");
Tabel Korespondensi Nilai Kunci
A 0x65 U 0x85
B 0x66 V 0x86
C 0x67 W 0x87
D 0x68 x 0x88
E 0x69 y 0x89
F 0x70 z 0x90
G 0x71 0 0x48
H 0x72 1 0x49
I 0x73 2 0x50
J 0x74 3 0x51
K 0x75 4 0x52
L 0x76 5 0x53
M 0x77 6 0x54
N 0x78 7 0x55
O 0x79 8 0x56
P 0x80 9 0x57
Q 0x81 ESC 0x1b
R 0x82 Ctrl 0x11
S 0x83 Shift 0x10
T 0x84 Masukkan 0xD
Jika Anda ingin menggunakan kombinasi kunci, Anda dapat menggunakan event.ctrlkey, event.shiftkey, event.altkey, event.altkey untuk menentukan apakah tombol CTRL, tombol shift dan tombol ALT telah ditekan.