views:

193

answers:

4

Url is here:

http://jqueryui.com/demos/dialog/#modal

Suppose this dialog contains two buttons:"OK" and "NO",

when "Enter" is pressed,I want the function binded with "OK" button to be called.

How to do that?

The dialog is something like this:

<table cellpadding="0" border="0" width="100%">
    <tr align="center">
        <td align="right">name/email:</td>
        <td><input type="text" id="name" /></td>
    </tr>
    <tr align="center">
        <td align="right" valign="top">password:</td>
        <td>
                <input type="text" id="psw" />
        </td>
    </tr>
    <tr align="center">
        <td></td>
        <td><span id="loginErr"></span></td>
    </tr>
</table>
+2  A: 

Hi,

Assign that "Ok" button to a tag and give it the focus (if the first thing doesn't work properly alone)

yoda
See my update,can't focus on the button.It should work when focusing on some <input >
Shore
A: 

No, that functionality isn't provided out of the box, you'll need to add it yourself or find a plugin which adds it (I'm not aware of one off the top of my head).

A possible solution is one which listens for the keyup event of the dialog and determines if it originated in an <input> element. If so, query the dialog's options for the buttons member and call the first button's handler function (a close approximation to clicking on the default button).

Ken Browning
+1  A: 

you can easy add your custom buttons by adding your option-object

buttons: {
'Button OK': function() {
  // Do something
 },
'Button No': function() {
  // Do something
}

to submit the form fields on key-press enter just do something like this:

$("#name").keydown(function(event){
    if(event.keyCode == "13") {
        // call the function for submit the form
    }
});

a complete source of all KeyCodes please find here: http://unixpapa.com/js/key.html

snuffer.ch
+1  A: 

either take the one suggested by snuffer-ch or use jQuery-Hotkeys plug in http://code.google.com/p/js-hotkeys/ which let you do:

$(document).bind('keydown', 'return', function(){
  //your code goes here
});
Tzury Bar Yochay