MVC3 input button triggered by Enter button

2019-04-10 23:32发布

问题:

I have an ASP.NET MVC 3 C#.NET web application and I want the Enter button to trigger a particular Save button on my form. How would I do that?

回答1:

What I would do is utilize JavaScript for this one. Handle the onkeypress even of the form. Like so:

<form onkeypress="yourKeyPressFunction(event)">
...
</form>

Then your yourKeyPressFunction() would look something like this:

function yourKeyPressFunction(e)
{
    if (e.keyCode == 13)
    {
        // submit or do what you'd like when you hit enter
    }

    // ... so on and so forth
}