I have a custom jQuery class to create popup list, but when i pass the HTML elements to class objects is undefined
!
I want to send element to class and set .keyup()
event for that and manipulate element value in .keyup()
function.
My code is:
$.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
{});
Code for using above class:
$(document).ready(function () {
var kpopup = new kpopup(
$("#clas_academy_id"),
$(".popup-picker"),
$("#clas_academy_id_hidden"),
$("#popup-result"),
'@Url.Action("searchAcademies","Academy")'
);
});
Maybe you can try this, press F12 in chrome or control+shift+k in Firefox to open the console and inspect the output. You can click on some of the console.log outputs to inspect the values of the objects logged.
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();
}
}
});
});
};
Note that I Capitalised your Kpopup object because that's the usual notation for constructor functions so your document ready you have to capitalise it as well:
$(document).ready(function () {
var kpopup = new Kpopup(
$("#clas_academy_id"),
$(".popup-picker"),
$("#clas_academy_id_hidden"),
$("#popup-result"),
'@Url.Action("searchAcademies","Academy")'
);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.