简体   繁体   English

jQueryUI对话框提交处理程序的参数从哪里来?

[英]Where are the arguments to the jQueryUI Dialog submit handler coming from?

Take a look at the following code: 看下面的代码:

  this.dialog({
                    width: 500,
                    height: 260,
                    title: "Setup database",
                    content: $("<form>").append(table),
                    buttons: {
                        submit: function(_alert, dialog) {
                            dialog.find("form").each(function() {
                                var arr = $(this).serializeArray();
                                var data = {
                                    mysql: true
                                };
                                var empty = false;
                                $(this).find("input").removeClass("error");
                                for (var k in arr) {
                                    if ($.trim(arr[k].value) !== "") {
                                        data[arr[k].name] = arr[k].value;
                                    } else {
                                        empty = true;
                                        $(this).find("input[name='" + arr[k].name + "']").each(function() {
                                            $(this).addClass("error");
                                        });
                                        break;
                                    }
                                }
                                if (!empty) {
                                    self.ajax({
                                        url: url,
                                        data: data
                                    }, function(result) {
                                        callback(result);
                                    }, function() {
                                        self.mysql(url, callback, _db_name, _db_user, _db_pass, is_dialog);
                                    });
                                }
                                _alert.remove();
                                if($.isFunction(callback_submit)) {
                                    callback_submit();
                                }
                            });
                        }
                    }
                });

There are two parameters passed into the anonymous function that is supposed to trigger when the button "submit" is clicked. 有两个传递到匿名函数中的参数,应该在单击“提交”按钮时触发。 But I have no idea where these parameters are supposed to come from. 但是我不知道这些参数应该从哪里来。 Can someone explain? 有人可以解释吗? Is this related to passing parameters to an anonymous function in Javascript in general? 一般而言,这与将参数传递给匿名函数有关吗?

I don't think you get any argument passed to you when a button event callback is fired on jquery-ui dialog box 我认为在jquery-ui对话框上触发按钮事件回调时,您不会传递任何参数给您

http://jsfiddle.net/3d7QC/1577/ http://jsfiddle.net/3d7QC/1577/

buttons: {
    "I've read and understand this": function() {
        console.log(arguments);
        // look at your console
        $(this).dialog("close");
    }

Only argument you get passed through to you is the customary jQuery event object. 唯一传递给您的参数是常规jQuery事件对象。

The first argument _alert is the JS event object that is passed to every event handler in JavaScript. 第一个参数_alert是JS事件对象,该对象传递给JavaScript中的每个事件处理程序。 This is not specific to jQuery. 这并非特定于jQuery。 javascript.info explains this as follows : javascript.info对此进行了如下解释

W3C way W3C方式

Browsers which follow W3C standards always pass the event object as the first argument for the handler. 遵循W3C标准的浏览器始终将事件对象作为处理程序的第一个参数传递。

For instance: 例如:

 element.onclick = function(event) { // process data from event } 

In the jQueryUI API reference they confirm that i jQueryUI API参考中,他们确认我

Specifies which buttons should be displayed on the dialog. 指定应在对话框上显示哪些按钮。 The context of the callback is the dialog element; 回调的上下文是dialog元素; if you need access to the button, it is available as the target of the event object. 如果需要访问按钮,则可以将其用作事件对象的目标。

I illustrated this in a fiddle . 用小提琴说明了这一点 Not sure what the second argument ( dialog in your case) does, though. 但是,不确定第二个参数(在您的情况下为dialog )的作用。 It's not passed in my example code. 它没有在我的示例代码中传递。

There should only be one parameter passed to submit, which is the event object of the button itself, when clicked. 单击时,只传递一个参数来提交,这是按钮本身的事件对象。 So the context set is the submit button, if you need to access the dialog and modify it, you can do so by accessing the event.target property. 因此,上下文集就是“提交”按钮,如果您需要访问对话框并对其进行修改,则可以通过访问event.target属性来实现。

  this.dialog({
  buttons: {
      submit: function(event) {
         $(event).dialog('close'); //is the same as...
         $(this).dialog('close');
      }
   });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM