0

我正在尝试学习构建自己的 jquery 插件。用一个简单的例子,但我在执行它时遇到错误。计划构建插件

贷款计算器

可以帮助我解决错误“

预期对象

我在 index.html 中的 javascript 是这样的......

$(function(){
    $('#btn').click(function() {
     var roi=getie();  //  ROI - rate of interest - value of i receved from function getie()
    $("#i").val(roi);  // input type 'text ' to display Interest (i) value
    });

});

和用于自定义功能的插件 codgin

盖蒂()

像这样分开

计算.js 文件

代码 :

(function($){
    //Attach this new method to jQuery
    $.fn.extend({ 
            //This is where you write your plugin's name
       getie: function() {
            //Iterate over the current set of matched elements
            return this.each(function() {
             var  i=20;
             return i;
             });
        }
    });


})(jQuery);

我正在使用 jquery-1.7.2.min.js 和 query-ui-1.8.20.custom.min.js 作为其他 js 文件。

4

3 回答 3

0

你应该打电话

roi = $('someSelector').getie()

“预期的对象”意味着您应该向函数传递一个对象,在这种情况下是 jQuery 对象或一些 jQuery 包装的集合。

于 2012-06-07T06:54:51.640 回答
0

随着$.fn.extend您扩展 jquery 并且不创建对象。所以你必须在一个 jquery 对象上调用它:

$(this).getie()
于 2012-06-07T06:55:40.283 回答
0

为什么你需要 getie 成为一个 jQuery 插件?您使用它的方式和/或它的常规功能就足够了

function getie(){
}
于 2012-06-07T07:14:37.667 回答