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

后端 未结 14 1285
不思量自难忘°
不思量自难忘° 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.

    0 讨论(0)
  • 2020-12-04 17:03

    I know this is an old thread, but I was searching for this exact functionality and was able to implement what I think is the best solution as I found all of the above to fall short a little.

    It is a combination of two answers above. Using an ID rather than relying on the find() function to find the button element always seems to be a much better choice to me.

    Also explicitly looking for the enter key to be pressed allows us to set focus to whatever element we want when the dialog is opened if desired. This just seems to allow for the most flexibility while satisfying the desire of triggering a specific button as 'default' when the enter key is pressed. I have also implemented a 'cancel' default as well.

    I hope this helps others looking for a good 'default' button solution for dialogs.

    $("#LoginBox").dialog({
       open: function(){
          $(this).keydown(function (event) {
             if (event.keyCode == 13) {
                $("#LogInButton").trigger("click");
                return false;
             }
             if (event.keyCode == 27) {
                $("#CancelButton").trigger("click");
                return false;
             }
          });
       },
       close: function(){
          $(this).dialog("destroy");
       },
       buttons: [
          {
             id: "LogInButton",
             text: "Log In",
             click: function(){
                //button functionality goes here
                $(this).dialog("destroy");
             }
          },
          {
             id: "CancelButton",
             text: "Cancel",
             click: function(){
                $(this).dialog("destroy");
             }
          }
       ]
    });
    
    0 讨论(0)
  • 2020-12-04 17:04

    This worked for me within the dialog using jquery 1.10.2

    dialog({
        focus: function() {
            $(this).on("keyup", function(e) {
                if (e.keyCode === 13) {
                    $(this).parent().find("button:eq(1)").trigger("click");
                    return false;
                }
            });
        },
    

    more options...

    0 讨论(0)
  • 2020-12-04 17:06

    A slight variation to use the buttons name as the selector. It reads a little better but there is obvious duplication with the button text string. Refactor to taste.

    $("#confirm-dialog").dialog({
        buttons: {
            "Cancel" : function(){},
            "OK" : function(){}
        },
        open: function() {
            $(this).siblings('.ui-dialog-buttonpane').find("button:contains('OK')").focus(); 
        }
    });
    
    0 讨论(0)
  • 2020-12-04 17:07

    In my case, none of the answers worked because I called .dialog on an empty div and added my buttons dynamically, so the $(this).html() would return nothing. So I couldn't call methods like parent() or siblings() and expect something in return. What I did was select the ui-dialog-buttonpane class directly and find the button element from there

    HTML

    <div id = "dialogexample">
    </div>
    

    Jquery

    $("#dialogexample").dialog({
        autoOpen: false,
        modal: true,
        open: function () {
            $('.ui-dialog-buttonpane').find('#otherbutton').focus();
        }
    });
    var buttons = {
        "MyButton" : {
            text: "Do Stuff",
            id: "dostuffbutton" 
        },
        "OtherButton" : {
            text: "Other Stuff",
            id: "otherbutton"
        }
    } 
    $("#dialogexample").dialog("option", "buttons", buttons);
    $("#dialogexample").dialog("open"); //the second (otherbutton), instead of
                                        //the first (dostuffbutton) button should be focused
    
    0 讨论(0)
  • 2020-12-04 17:09

    Another option that gives you more control over all buttons in the dialog is to add them as an array of buttons. Then in the open event you can get the buttons by id and do whatever you want (including set the focus)

    $('#myDialog').dialog({
        buttons: [  
                    {
                        id: "btnCancel",
                        text: "Cancel",
                        click: function(){
                            $(this).dialog('close');
                        }
                    },
                    {
                        id: "btnOne",
                        text: "Print One",
                        click: function () {
                            SomeFunction(1);
                        }
                    },
                    {
                        id: "btnTwo",
                        text: "Print Two",
                        click: function(){
                            SomeFunction(0);
                        }
                    }
                ],
        open: function () {
            if ($('#hiddenBool').val() != 'True') {
                $('#btnOne').hide();
            }
            $("#btnTwo").focus();
        }
    });
    
    0 讨论(0)
提交回复
热议问题