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

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

    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
    

提交回复
热议问题