如何从jquery小部件函数获取返回值 [英] How to get a return value from a jquery widget function

查看:75
本文介绍了如何从jquery小部件函数获取返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个带有公共功能的jQuery小部件

I have write a jquery widget with a public function

 $.widget("UI.MyWidget", {
        vars: {
            retvalue:0
                  },
        options: {
            Orgintop: 0,
            Orginleft: 0,
            Orginheight: 0,
            Orginwidth: 0
        },
        _create: function () {
            this._renderElement();

        },
        getControlValue: function () {
            this.vars. retvalue=45+5;
            return this.vars. retvalue;
        }
});

,我需要从另一个脚本调用此公共函数getControlValue.我该如何称呼它并获得那个返回值.我是widget的新手.谁能帮忙.

and I need to call this public function getControlValue from another script. How can I call this and get that return value. I am new in widget. can any one help.

谢谢.

推荐答案

您可以在this.element.data()处设置getControlValue,从集合.data()中返回命名函数,并且将this设置为可调用的小部件通过引用设置小部件的元素的.data().

You can set getControlValue at .data() of this.element, return named function from set .data() with this set to widget which is callable by referencing .data() of element where widget is set.

$.widget("UI.MyWidget", {
  vars: {
    retvalue: 0
  },
  options: {
    Orgintop: 0,
    Orginleft: 0,
    Orginheight: 0,
    Orginwidth: 0
  },
  _create: function() {
    this.element.data("_getControlValue", this.getControlValue());
    // this._renderElement();
  },
  getControlValue: function() {
    var fn = function() {
      this.vars.retvalue = 45 + 5;
      return this.vars.retvalue;
    }
    return fn.bind(this)
  }
});

var widget = $("div").MyWidget();
console.log(widget.data()._getControlValue());

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js" integrity="sha256-eGE6blurk5sHj+rmkfsGYeKyZx3M4bG+ZlFyA7Kns7E=" crossorigin="anonymous"></script>
<div></div>

这篇关于如何从jquery小部件函数获取返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆