admin管理员组文章数量:1323199
I have the following code:
<head>
<script language="javascript">
function subimiti(event){
alert("Tipo do submit: "+event);
}
</script>
</head>
<body>
<form id="f1" name="form1" onsubmit="subimiti(event)" action=";>
<input type="text" id="meuId" value="Teste"/>
<input id="butao" type="submit" value="Subimeta u fórmi trem bão!!"/>
</form>
In my JavaScript function I want to detect the event that causes the form submit. If it was by an enter key in my text field, or if it was by clicking on the button.
I have the following code:
<head>
<script language="javascript">
function subimiti(event){
alert("Tipo do submit: "+event);
}
</script>
</head>
<body>
<form id="f1" name="form1" onsubmit="subimiti(event)" action="http://www.google.">
<input type="text" id="meuId" value="Teste"/>
<input id="butao" type="submit" value="Subimeta u fórmi trem bão!!"/>
</form>
In my JavaScript function I want to detect the event that causes the form submit. If it was by an enter key in my text field, or if it was by clicking on the button.
Share Improve this question edited Sep 25, 2021 at 17:38 John Kary 7,2721 gold badge27 silver badges24 bronze badges asked Jun 26, 2013 at 20:04 Mateus ViccariMateus Viccari 7,70917 gold badges66 silver badges102 bronze badges 1- possible duplicate of Determine which element submitted a form from within onsubmit – dsgriffin Commented Jun 26, 2013 at 20:07
1 Answer
Reset to default 5Since the keypress event fires the click event when there's a submit button, the only workaround that I can think of is having a type=button instead.
Pure javascript (Fiddle):
<form id="f1" name="form1" action="" onsubmit="subimiti(this)" method="POST">
<input type="text" id="meuId" value="Test" onkeypress="setEvent(event)"/>
<input id="butao" type="button" onclick="setEvent(event)" value="Subimeta u formi trem bao!!"/>
</form>
function subimiti(form)
{
event.preventDefault();
alert(form.getAttribute('event'));
}
function setEvent(event)
{
if(event.type == 'click')
{
document.form1.setAttribute('event','click');
subimiti(document.form1);
}
else if (event.keyCode == 13)
{
document.form1.setAttribute('event','keypress');
}
}
Using jQuery (cleaner/easier code in my opinion):
$(function(){
$('form').submit(function(){
alert($(this).attr('event'));
});
$("input#butao").on('click', function(e) {
$("form").attr("event", "click").submit();
});
$("input").on('keypress', function(e) {
if (e.which == 13)
{
$("form").attr("event", "keypress");
}
});
});
There's a click listener for butao only and enter listener for any input.
<form id="f1" name="form1" action="">
<input type="text" id="meuId" value="Teste"/>
<input id="butao" type="button" value="Subimeta u formi trem bao!!"/>
</form>
Fiddle (added preventDefault to fiddle so you could see the results without actually submitting)
本文标签: javascriptDetect type of submitif it was by enter key or button clickStack Overflow
版权声明:本文标题:javascript - Detect type of submit, if it was by enter key or button click - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742122308a2421773.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论