jquery-ui Dialog: Make a button in the dialog the default action (Enter key)

后端 未结 14 1331
不思量自难忘°
不思量自难忘° 2020-12-04 16:27

In a jquery modal dialog, is there a way to select a button as the default action (action to execute when the user presses enter)?

Example of jquery web site: jquer

14条回答
  •  孤街浪徒
    2020-12-04 16:58

    I like this one (it is working for me), which leaves the focus where I wanted to be (a text box)

        $("#logonDialog").keydown(function (event) {
            if (event.keyCode == $.ui.keyCode.ENTER) {
                $(this).parent()
                       .find("button:eq(0)").trigger("click");
                return false;
            }
        });
    

    However, this is working just for one button (Ok button), if needed ':eq(n)' could be set to select other button.

    Note: I added a new line returning false to prevent event bubbling when the enter key is handled, I hope it helps better than before.

提交回复
热议问题