Handle user hitting 'Enter' key in a ASP.NET MVC web site

后端 未结 6 2103
星月不相逢
星月不相逢 2020-12-29 03:32

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

    
    &         


        
6条回答
  •  长发绾君心
    2020-12-29 04:05

    First off, this is wrong:

    
    
    
    

    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);
    

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

提交回复
热议问题