最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Is it possible to set an HTML button as default inside a form? - Stack Overflow

programmeradmin4浏览0评论

I'm working on a asp page and I have several buttons in my page, some of them are asp buttons, other are HTML input[type=button] elements. HTML buttons are used to make AJAX calls.

When the user press ENTER I need to set as default button one of these HTML buttons, but at the moment the first button in the page is trigged.

Is there a way to set a plain HTML button as default on my form?

I'm working on a asp page and I have several buttons in my page, some of them are asp buttons, other are HTML input[type=button] elements. HTML buttons are used to make AJAX calls.

When the user press ENTER I need to set as default button one of these HTML buttons, but at the moment the first button in the page is trigged.

Is there a way to set a plain HTML button as default on my form?

Share Improve this question asked Nov 30, 2012 at 10:49 daviooohdavioooh 24.7k47 gold badges170 silver badges258 bronze badges 2
  • stackoverflow./questions/1963245/… – Habib Commented Nov 30, 2012 at 10:51
  • Move it to the "first" position? – Cerbrus Commented Nov 30, 2012 at 10:51
Add a ment  | 

4 Answers 4

Reset to default 6

Try this :

   // JavaScript Code
   var body = document.getElementsByTagName('body')[0];
    body.onkeydown = function (e) {
        if (e.keyCode === 13) {  //enter key code
         // call your html button onclick code here
        }
    }

I'm using JQuery, so I modified the code in the Mohit Pandey answer:

$("body").keypress(function (event) {
    if (event.which == 13) {
        event.preventDefault();
        // my function
    }
});

read this article http://www.codeproject./Articles/35180/How-To-set-Default-Button-for-ENTER-key-pressed-ev

you can use it in somewhat this way

<form id="form1" runat="server" defaultbutton="Button1" >

Can you not put the form inside a panel and use the defaultbutton property

<asp:Panel runat="server" DefaultButton="btnSomething"> 
...
</asp:Panel>
发布评论

评论列表(0)

  1. 暂无评论