[英]Where are the arguments to the jQueryUI Dialog submit handler coming from?
看下面的代碼:
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();
}
});
}
}
});
有兩個傳遞到匿名函數中的參數,應該在單擊“提交”按鈕時觸發。 但是我不知道這些參數應該從哪里來。 有人可以解釋嗎? 一般而言,這與將參數傳遞給匿名函數有關嗎?
我認為在jquery-ui對話框上觸發按鈕事件回調時,您不會傳遞任何參數給您
http://jsfiddle.net/3d7QC/1577/
buttons: {
"I've read and understand this": function() {
console.log(arguments);
// look at your console
$(this).dialog("close");
}
唯一傳遞給您的參數是常規jQuery事件對象。
第一個參數_alert
是JS事件對象,該對象傳遞給JavaScript中的每個事件處理程序。 這並非特定於jQuery。 javascript.info對此進行了如下解釋 :
W3C方式
遵循W3C標准的瀏覽器始終將事件對象作為處理程序的第一個參數傳遞。
例如:
element.onclick = function(event) { // process data from event }
在jQueryUI API參考中,他們確認我
指定應在對話框上顯示哪些按鈕。 回調的上下文是dialog元素; 如果需要訪問按鈕,則可以將其用作事件對象的目標。
我用小提琴說明了這一點 。 但是,不確定第二個參數(在您的情況下為dialog
)的作用。 它沒有在我的示例代碼中傳遞。
單擊時,只傳遞一個參數來提交,這是按鈕本身的事件對象。 因此,上下文集就是“提交”按鈕,如果您需要訪問對話框並對其進行修改,則可以通過訪問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.