简体   繁体   English

当有两个按钮时,如何使用回车键提交

[英]How to submit using the enter key when there are two buttons

I have two different input fields with their own respective submit buttons and I want the user to be able to enter their input into either of the text boxes and then hit the enter key and have the same effect as if they were to click on the submit button. 我有两个不同的输入字段及其各自的提交按钮,我希望用户能够将其输入输入到任一文本框中,然后按Enter键并具有与单击提交相同的效果按钮。 Currently, typing something into the text box and hitting the enter key does nothing. 目前,在文本框中键入内容并按Enter键不会执行任何操作。 I'm using HTML and JavaScript and ASP .NET MVC3. 我正在使用HTML和JavaScript以及ASP .NET MVC3。 My code looks like this: 我的代码看起来像这样:

The HTML HTML

<table>
<tr>
    <td><span class="labelText">Text1</span></td> <td><input id="text1" type="text" placeholder="Enter Text1"/> </td> <td style="width: 110px; text-align: left; margin-left: 5px"><button class="medium awesome blue" id="sendText1">Send Text1</button></td>
</tr>
<tr>
   <td> <span class="labelText">Text2</span> </td> <td><input id="text2" type="text" placeholder="Enter Text2"/> </td>   <td style="width: 110px; text-align: left; margin-left: 5px"> <button class="medium awesome blue" id="sendText2">Send Text2</button> </td>
</tr>

The JavaScript JavaScript

$('#sendText1').click(function () {
        $('#text2').val("");
        var text1 = $("#text1").val();
        $('#loadingUsageInfo').css({ 'visibility': 'visible' });
        if (text1 == "" || text1 == 'Enter Text1') {
            alert('Enter Text First');
            return;
        }
        $.ajax({
            url: '@Url.Action("ShowResult1", "Folder")',
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify({ input1: text1 }),
            traditional: true,
            success: function (result) {
                $('#info').html(result);
            }
        });
    });

    $('#sendText2').click(function () {
        $('#text2').val("");
        var text2 = $("#text2").val();
        $('#loadingUsageInfo').css({ 'visibility': 'visible' });
        if (text2 == "" || text2 == 'Enter Text2') {
            alert('Enter Text First');
            return;
        }

        $.ajax({
            url: '@Url.Action("ShowResult2", "Folder")',
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify({ input2: text2 }),
            traditional: true,
            success: function (result) {
                $('#info').html(result);
            }
        });
    });

Thanks in advance! 提前致谢!

Add this below your js code 在你的js代码下面添加这个

$('#text1, #text2').keypress(function(e) {
  if (e.which == 13) {
    $('#sendText1, #sendText2').trigger('click');
  }       
}​​​​​​);

Trigger - Executes all handlers and behaviors attached to the matched elements for the given event type. 触发器 - 执行附加到给定事件类型的匹配元素的所有处理程序和行为。

So we're binding both text inputs on keypress and calling all bindings for the buttons. 所以我们在按键上绑定文本输入并调用按钮的所有绑定。

Also see how that works on the jsFiddle 另请参阅jsFiddle的工作原理

Your text fields do not know that they are supposed to submit values somewhere. 您的文本字段不知道它们应该在某处提交值。 You may let them know by surrounding them by form tag. 您可以通过form标记将它们包围来告诉他们。

I ended up doing something like this. 我最终做了这样的事情。

$("#textbox").keypress(function (e) {
    var code = (e.keyCode ? e.keyCode : e.which);
    if (code === 13) {
        e.preventDefault();
        $("#btn").trigger("click");
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM