I have an HTML input which is a textfield. When I am pressing the enter, this will call the submit, which is normal.
我有一个HTML输入,这是一个文本字段。当我按下回车时,这将调用提交,这是正常的。
Now, I would like to do something else when Enter is clicked on that textBox.
现在,我想在单击该文本框上的Enter时执行其他操作。
I am trying something like that:
我正在尝试这样的事情:
<input type="text"
id="first_page"
onPaste=""
onkeydown="if (event.keyCode == 13) alert('enter')" />
The alert works well but the submit is still done. My page is reloading after that.
警报运行良好,但提交仍然完成。之后我的页面正在重新加载。
Could you help me please.
请问你能帮帮我吗。
8 个解决方案
#1
22
write return false;
and check it
写返回false;并检查它
#2
22
Using jquery
使用jquery
<script>
$('#first_page').keypress(function(e) {
if (e.keyCode == '13') {
e.preventDefault();
//your code here
}
});
</script>
using javascript
使用javascript
<input type="text" id="first_page" onPaste="" onkeydown="if (event.keyCode == 13) { alert('enter');return false;}" />
#3
4
Other option would be using onkeypress
其他选择是使用onkeypress
<input type="text" id="first_page" onPaste="" onkeypress="if (event.keyCode == 13) {alert('enter'); return false;}" />
#4
2
You could try the below script :
您可以尝试以下脚本:
<script>
function checkEnter(event)
{
if (event.keyCode == 13)
{
alert('enter')
return false;
}
}
And the Html:
和Html:
<input type="text" id="first_page" onPaste="" onkeydown="return checkEnter(event);" />
#5
1
You should be returning false
in the keydown
event handler function in order to prevent further processing of the event.
您应该在keydown事件处理函数中返回false,以防止进一步处理事件。
Also, you might find the following question useful: Prevent Users from submitting form by hitting enter.
此外,您可能会发现以下问题很有用:通过按Enter键阻止用户提交表单。
#6
1
write the following code after alert('enter')
警告后写下面的代码('enter')
return false;
#7
0
Use this, it worked in mine.
使用它,它在我的工作。
Replace on the keydown:
替换keydown:
onkeydown="if (event.keyCode == 13 || event.which == 13) event.preventDefault();"
#8
-2
using jquery you can avoid all submiting or some element
使用jquery可以避免所有提交或某些元素
$("#form").submit(function(e) {
e.preventDefault();
});
#1
22
write return false;
and check it
写返回false;并检查它
#2
22
Using jquery
使用jquery
<script>
$('#first_page').keypress(function(e) {
if (e.keyCode == '13') {
e.preventDefault();
//your code here
}
});
</script>
using javascript
使用javascript
<input type="text" id="first_page" onPaste="" onkeydown="if (event.keyCode == 13) { alert('enter');return false;}" />
#3
4
Other option would be using onkeypress
其他选择是使用onkeypress
<input type="text" id="first_page" onPaste="" onkeypress="if (event.keyCode == 13) {alert('enter'); return false;}" />
#4
2
You could try the below script :
您可以尝试以下脚本:
<script>
function checkEnter(event)
{
if (event.keyCode == 13)
{
alert('enter')
return false;
}
}
And the Html:
和Html:
<input type="text" id="first_page" onPaste="" onkeydown="return checkEnter(event);" />
#5
1
You should be returning false
in the keydown
event handler function in order to prevent further processing of the event.
您应该在keydown事件处理函数中返回false,以防止进一步处理事件。
Also, you might find the following question useful: Prevent Users from submitting form by hitting enter.
此外,您可能会发现以下问题很有用:通过按Enter键阻止用户提交表单。
#6
1
write the following code after alert('enter')
警告后写下面的代码('enter')
return false;
#7
0
Use this, it worked in mine.
使用它,它在我的工作。
Replace on the keydown:
替换keydown:
onkeydown="if (event.keyCode == 13 || event.which == 13) event.preventDefault();"
#8
-2
using jquery you can avoid all submiting or some element
使用jquery可以避免所有提交或某些元素
$("#form").submit(function(e) {
e.preventDefault();
});