提交表单时没有提交按钮吗?(复制)

时间:2022-11-24 10:28:41

Possible Duplicate:
HTML: Submitting a form by pressing enter without a submit button

可能的副本:HTML:通过不带提交按钮的按下enter来提交表单

How can I submit a form with just the Enter key on a text input field, without having to add a submit button?

如何只在文本输入字段中使用Enter键提交表单,而不需要添加submit按钮?

I remember this worked in the past, but I tested it now and the form doesn't get submitted unless there's a submit-type input field inside it.

我记得这在过去是有用的,但是我现在测试了它,除非表单内部有提交类型的输入字段,否则不会提交表单。

4 个解决方案

#1


194  

$("input").keypress(function(event) {
    if (event.which == 13) {
        event.preventDefault();
        $("form").submit();
    }
});

#2


47  

Change #form to your form's ID

将#窗体更改为窗体的ID

$('#form input').keydown(function(e) {
    if (e.keyCode == 13) {
        $('#form').submit();
    }
});

Or alternatively

或者

$('input').keydown(function(e) {
    if (e.keyCode == 13) {
        $(this).closest('form').submit();
    }
});

#3


14  

Jay Gilford's answer will work, but I think really the easiest way is to just slap a display: none; on a submit button in the form.

杰伊·吉尔福德的答案是行得通的,但我认为最简单的办法就是拍板:没有;在表单的提交按钮上。

#4


5  

@JayGuilford's answer is a good one, but if you don't want a JS dependency, you could use a submit element and simply hide it using display: none;.

@JayGuilford的答案很好,但是如果您不想要一个JS依赖项,您可以使用提交元素并使用display: none隐藏它;

#1


194  

$("input").keypress(function(event) {
    if (event.which == 13) {
        event.preventDefault();
        $("form").submit();
    }
});

#2


47  

Change #form to your form's ID

将#窗体更改为窗体的ID

$('#form input').keydown(function(e) {
    if (e.keyCode == 13) {
        $('#form').submit();
    }
});

Or alternatively

或者

$('input').keydown(function(e) {
    if (e.keyCode == 13) {
        $(this).closest('form').submit();
    }
});

#3


14  

Jay Gilford's answer will work, but I think really the easiest way is to just slap a display: none; on a submit button in the form.

杰伊·吉尔福德的答案是行得通的,但我认为最简单的办法就是拍板:没有;在表单的提交按钮上。

#4


5  

@JayGuilford's answer is a good one, but if you don't want a JS dependency, you could use a submit element and simply hide it using display: none;.

@JayGuilford的答案很好,但是如果您不想要一个JS依赖项,您可以使用提交元素并使用display: none隐藏它;