处理用户点击一个ASP.NET MVC的网站“Enter”键 [英] Handle user hitting 'Enter' key in a ASP.NET MVC web site

查看:119
本文介绍了处理用户点击一个ASP.NET MVC的网站“Enter”键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作具有多个提交按钮的ASP.NET MVC网站。即。

I am working on a ASP.NET MVC web site that has multiple submit buttons. i.e.

    <input type="submit" name="SubmitButton" value="Reset" />
    <input type="submit" name="SubmitButton" value="OK" />
    <input type="submit" name="SubmitButton" value="Back" />

我需要让用户可以快速提交由pressing的Enter键的形式。 HTML标准似乎指定的第一个提交按钮会假设,如果一个用户preSS的Enter键。不过,我需要做的第二个按钮(即OK)按钮的默认按钮和原因,我甚至不想谈,改变按键顺序是不是一种选择。

I need to allow users to quickly submitting the form by pressing the 'Enter' key. HTML standard seems to specify that the first submit button will be assumed if a user press the 'Enter' key. However, I need to make the 2nd button (i.e. the "OK") button the default button and for reasons I don't even want to talk about, changing the button order is not an option.

我GOOGLE了四周,我发现有关使用此页帖子 .Form.DefaultButton在ASP.NET但这并不与ASP.NET MVC工作。

I googled around and I found this post about using Page.Form.DefaultButton in ASP.NET but this doesn't work with ASP.NET MVC.

我也试过下面的JavaScript解决方案,同时它工作在Chrome,但在IE6不工作

I also tried the following javascript solution, while it works in Chrome but doesn't work in IE6

    $('body').keypress(function(e) {
       if (e.which === 13) {
          $("input[value='OK']").trigger('click');
       }
    });

我能想到一些很极端的解决方案,如形式通过每一个去控制一个连接上述功能给他们。不过,我不认为这是一个非常巧妙的解决办法,所以我想知道有没有人有一个更好的解决方案?

I can think of some really extreme solutions such as going through every single controls in the form an attach the above function to them. However, I don't think that's a very neat solution so I am wondering has anyone got a better solution?

推荐答案

首先,这是错误的:

<input type="submit" name="SubmitButton" value="Reset" />
<input type="submit" name="SubmitButton" value="OK" />
<input type="submit" name="SubmitButton" value="Back" />

他们三个都提交按钮。复位的类型=复位的输入。得到排序。其次重要的是,我已经成功地实现类似的东西,而且它适用于IE6。试试这个:

All three of them are submit buttons. A reset is an input of type="reset". Get that sorted. Second of all, I've successfully implemented something like that, and it works on IE6. Try this:

    function keypressHandler(e)
    {
        if(e.which == 13) {
            e.preventDefault(); //stops default action: submitting form
            $(this).blur();
            $('#SubmitButton').focus().click();//give your submit an ID
        }
    }

    $('#myForm').keypress(keypressHandler);

焦点()一部分,使按钮显示为pressed当用户presses进入。很漂亮的。

The focus() part makes the button appear to be pressed when the user presses enter. Quite nifty.

这篇关于处理用户点击一个ASP.NET MVC的网站“Enter”键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆