为什么我的 click() 到 on() 转换没有按预期工作?

发布于 2025-01-11 11:54:31 字数 1598 浏览 0 评论 0原文

jquery 中的 .click 已被弃用。所以我对现有的代码进行了修改,但是,似乎存在一些问题。

    GetPageButtons() {
    let $saveButton = $("<input />", {
        type: "button",
        value: "Save",
        class: "button large save",
      }),
      $cancelButton = $("<input />", {
        type: "button",
        value: "Cancel",
        class: "button large cancel",
      });
    $saveButton.click(() => {
      this.UpdateRecord();
    });
    $cancelButton.click(() => {
      this.modal.success = true;
      let _parent = this;
      $.each(_parent.fields, function (i, field) {
        delete this.newValue;
        this.element.val("");
      });
    });
    return [$cancelButton, $saveButton];
  }

上面的代码是有效的,尽管我的 linter 抱怨说它已被弃用。

  GetPageButtons() {
    let $saveButton = $("<input />", {
        type: "button",
        value: "Save",
        class: "button large save",
      }),
      $cancelButton = $("<input />", {
        type: "button",
        value: "Cancel",
        class: "button large cancel",
      });
    $saveButton.on("click", function() {
      this.UpdateRecord();
    });
    $cancelButton.on("click", function() {
      this.modal.success = true;
      let _parent = this;
      $.each(_parent.fields, function (i, field) {
        delete this.newValue;
        $( this ).element.val("");
      });
    });
    return [$cancelButton, $saveButton];
  }

然而,对建议使用 .on('click', fn){} 的重构不起作用。我收到一条错误消息:

“未捕获的类型错误:this.UpdateRecord 不是函数”。

为什么?它的工作方式应该与 .click 相同,.click 是 .on 的简写。 有什么建议吗???

The .click in jquery is deprecated. So I made amendments to the existing code, however, there seems to be some issue.

    GetPageButtons() {
    let $saveButton = $("<input />", {
        type: "button",
        value: "Save",
        class: "button large save",
      }),
      $cancelButton = $("<input />", {
        type: "button",
        value: "Cancel",
        class: "button large cancel",
      });
    $saveButton.click(() => {
      this.UpdateRecord();
    });
    $cancelButton.click(() => {
      this.modal.success = true;
      let _parent = this;
      $.each(_parent.fields, function (i, field) {
        delete this.newValue;
        this.element.val("");
      });
    });
    return [$cancelButton, $saveButton];
  }

The above code works, although my linter complains saying it deprecated.

  GetPageButtons() {
    let $saveButton = $("<input />", {
        type: "button",
        value: "Save",
        class: "button large save",
      }),
      $cancelButton = $("<input />", {
        type: "button",
        value: "Cancel",
        class: "button large cancel",
      });
    $saveButton.on("click", function() {
      this.UpdateRecord();
    });
    $cancelButton.on("click", function() {
      this.modal.success = true;
      let _parent = this;
      $.each(_parent.fields, function (i, field) {
        delete this.newValue;
        $( this ).element.val("");
      });
    });
    return [$cancelButton, $saveButton];
  }

The refactor to the suggested use of .on('click', fn){} however is not functioning. I get an error that says:

"Uncaught TypeError: this.UpdateRecord is not a function".

Why? It should work the same as the .click which is shorthand for .on.
Any suggestions???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岛歌少女 2025-01-18 11:54:31

这是行不通的,因为您使用的是普通函数,而不是使用箭头函数的原始代码。

这意味着函数内的 this 指的是不同的东西。

将其更改为

$saveButton.on("click", () => {
  this.UpdateRecord();
});
$cancelButton.on("click", () => {
  this.modal.success = true;
  let _parent = this;
  $.each(_parent.fields, function(i, field) {
    delete this.newValue;
    $(this).element.val("");
  });
});

,它应该像原来一样工作。

This does not work, because you are using normal function in contrast to the original code that used arrow function.

This means that the this inside the functions refers to something different.

Change it to

$saveButton.on("click", () => {
  this.UpdateRecord();
});
$cancelButton.on("click", () => {
  this.modal.success = true;
  let _parent = this;
  $.each(_parent.fields, function(i, field) {
    delete this.newValue;
    $(this).element.val("");
  });
});

and it should work as originally.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文