2014-08-28 62 views
0

我在asp.net mvc中工作,我想製作jquery插件。 我嘗試在論壇上找到解決方案,但始終得到相同的錯誤。 我寫的是這樣的:

(function($) { 
    $.fn.extend({ 
     fillcombo: function(options) { 
      var datadef = { 
           region:"", 
           num:"", 
           filter1:"", 
           filter2:"" 
      }; 

      options = $.extend(datadef, options); 
      $.ajax({ 
       url:  "Main/SyncCombo", 
       dataType: "json", 
       type:  "GET", 
       data:  datadef ,    
       success: function (response) { 

        this.empty(); 
        var optionhtml1 = '<option value="' + 0 + '"></option>'; 
        this.append(optionhtml1); 
        $.each(response, function (i) { 
         var optionhtml = '<option value="' + response[i].CODE.toString() + '">' + response[i].DESCR + '</option>'; 
         this.append(optionhtml); 
        }); 
       } 
      }); 

     } 
    }); 
})(jQuery); 

後來我想與調用它:

$('#cbBox').fillcombo({ region: region, num: num, filter1: filter1, filter2: filter2 }); 

凡區域,NUM等是我需要傳遞給阿賈克斯...... 變量,但螢火蟲我總是得到錯誤

TypeError: $(...).fillcombo is not a function 


...x').fillcombo({ region: region, num: num, filter1: filter1, filter2: filter2 }) 

我補充一點,我不知道如何在AJAX定義數據時,我寫的插件,我嘗試這種方式,像區域,NUM,等我得到相同的E rror ... 這是我第一次寫jQuery插件,我沒有線索,我犯了錯誤。 請幫助.. TNX

+2

已添加所需的js您的網頁上的文件? – 2014-08-28 05:52:03

+0

是的,我是。這個文件中的其他一些文件在調用時工作,但它們不是插件... – Frink 2014-08-28 05:52:55

+0

嘗試直接指定它而不是'extend'。像這樣:'$ .fn.fillcombo ='。 – 2014-08-28 06:45:19

回答

0

我找到解決辦法,我從mozzila只是清除緩存後,我得到第二個錯誤this.empty()然後我定義var self = this;並使用自己的不是這個......