[英]Use keyup function in custom jQuery class
我有一個自定義的jQuery類來創建彈出列表,但是當我將HTML元素傳遞給類對象時,它是undefined
!
我想將元素發送到類並.keyup()
設置.keyup()
事件,並在.keyup()
函數中操縱元素值。
我的代碼是:
$.Class('kpopup', {
// static
init: function (e, p, h, r, u) {
url = u;
hostElement = e;
popupElement = p;
popupResult = r;
hiddenElement = h;
$(hostElement).keyup(function () {
$.ajax({
url: url,
data: { "value": $(hostElement).val() },
type: 'POST',
success: function (dt) {
if (dt != "") {
$(popupResult).html(dt);
$(hostElement).popupDiv($(popupElement));
} else {
$(popupElement).hide();
}
}
});
});
}
},
//Prototypes
{});
使用上述類的代碼:
$(document).ready(function () {
var kpopup = new kpopup(
$("#clas_academy_id"),
$(".popup-picker"),
$("#clas_academy_id_hidden"),
$("#popup-result"),
'@Url.Action("searchAcademies","Academy")'
);
});
也許您可以嘗試此操作,請在chrome中按F12或在Firefox中按Control + Shift + k打開控制台並檢查輸出。 您可以單擊某些console.log輸出,以檢查記錄的對象的值。
Kpopup = function(e, p, h, r, u) {
console.log("creating Kpopup with parameters:", e, p, h, r, u);
this.url = u;
this.hostElement = e;
this.popupElement = p;
tis.popupResult = r;
this.hiddenElement = h;
this.init();
};
Kpopup.prototype.init = function() {
console.log("And this is:", this);
console.log("And this hostElement is:", this.hostElement);
var pResult = this.popupResult;
var hElement = this.hostElement;
var pElement = this.popupElement;
this.hostElement.keyup(function() {
$.ajax({
url: this.url,
data: {"value": hElement.val()},
type: 'POST',
success: function(dt) {
if (dt !== "") {
pResult.html(dt);
hElement.popupDiv(pElement);
} else {
pElement.hide();
}
}
});
});
};
請注意,我大寫了Kpopup對象,因為這是構造函數的常用符號,因此准備好文檔時也必須將其大寫:
$(document).ready(function () {
var kpopup = new Kpopup(
$("#clas_academy_id"),
$(".popup-picker"),
$("#clas_academy_id_hidden"),
$("#popup-result"),
'@Url.Action("searchAcademies","Academy")'
);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.