0

I want to detect if enter was used in html textfield i load into the Dialog.

If enter was pressed i want to simulate a click on the Save-Button.

i tried many things, basically pushing this code around:

 $(document).keypress(function(e) {
      if (e.keyCode == $.ui.keyCode.ENTER) {
        $("#resultTextArea").val("Enter gedrueckt");
            $(this).find("button:saveBtn").trigger("click");                                                                        
      }
});

Below is the AUI-Dialog (in which i tried above code)

          AUI().use('aui-dialog', 'liferay-portlet-url', function(A) {
                var dialog = new A.Dialog({
                                            title: 'New title',
                                            centered: true,
                                            modal: true,
                                            width: 500,
                                            height: 200,
                                            bodyContent:AUI().one('#addTestDialog'),
                                            buttons: [ 
                                                       { label : "Save" , id : "saveBtn" , handler : function(){ 
                                                            saveStuff();
                                                            this.close();
                                                        }
                                                       }
                                                        ],

                                            open: function() {
                                                $(document).keypress(function(e) {
                                                    if (e.keyCode == $.ui.keyCode.ENTER) {
                                                         $("#resultTextArea").val("Enter gedrueckt");
                                                        $(this).find("button:saveBtn").trigger("click");                                                                        
                                                    }
                                                });
                                            }

                }).render();
            });

And this is the html part that defines the body of the dialog:

<div style="display:none;">
  <div id="addTestDialog">

   <portlet:actionURL name="addNote" var="addNoteURL" />
    <br>
    <table style="width:100%;">
    <tr>
        <td>Name:</td>
        <td><input id="textInput" style="width:98%;height:20px;"></input></td>
    </tr>       
    </table> 
  </div>
</div>

I checked back with the API of AlloyUI and found this: http://deploy.alloyui.com/api/classes/Dialog.html

Under Attributes: There is no open which is used in every tutorial/code i found. Therefore this cannot work (at least i think so). i tested initialize but this wont work either.

thank you for any help/hints.

4

3 回答 3

1

你可以使用jQuery来完成这个任务

IE

$("#saveBtn").keypress(function(event){
  var keycode = (event.keyCode ? event.keyCode : event.which);
    if (keycode == '13') {
      // do what you want
    }
});

高温高压

于 2013-07-18T11:28:26.063 回答
1

它现在可以工作了,这是我的代码(在按下输入时,它与 savebtn 执行相同的功能并关闭对话框):

$(document).ready(function(){
  $("#scenario_add_btn").click(function(){             
     AUI().use('aui-dialog', 'liferay-portlet-url', function(A) {
       var dialog = new A.Dialog({
                                            title: 'New title',
                                            centered: true,
                                            modal: true,
                                            width: 500,
                                            height: 200,
                                            bodyContent:AUI().one('#addTestDialog'),
                                            buttons: [ 
                                                       { label : "Save" , id : "saveBtn" , handler : function(){ 
                                                            saveStuff();
                                                            this.close();
                                                        }
                                                       }
                                                        ]
                }).render();
                $("#textInput").keypress(function(event) {
                     var keycode = (event.keyCode ? event.keyCode : event.which);
                    if (keycode == '13') {
                        saveStuff();
                        dialog.close();
                    }
                  });

            });



     });/*btn.click*/
});/*document ready*/
于 2013-07-18T14:18:23.450 回答
0

keyCodefor enter13尝试替换$.ui.keyCode.ENTER13并检查它是否有效。

于 2013-07-18T09:48:20.643 回答