繁体   English   中英

在自定义jQuery类中使用keyup函数

[英]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.

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