最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to send parameters to jquery plugin function - Stack Overflow

programmeradmin2浏览0评论

I want to pass values to jquery plugin function.

This is my plugin:

(function ( $ ) {

    $.fn.myplugin = function(name,value) {
        alert(name + " , " + value )
    };

}( jQuery ));

And this is my call:

$('#wrapper').myplugin({name:'test',value:'big_test'});

I can't receive any data in my plugin. why?

I want to pass values to jquery plugin function.

This is my plugin:

(function ( $ ) {

    $.fn.myplugin = function(name,value) {
        alert(name + " , " + value )
    };

}( jQuery ));

And this is my call:

$('#wrapper').myplugin({name:'test',value:'big_test'});

I can't receive any data in my plugin. why?

Share Improve this question asked Jun 14, 2013 at 20:59 user123_456user123_456 5,82526 gold badges87 silver badges142 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

when you are sending data as object then receive it as object and change your plugin code like

$.fn.myplugin = function(data) {
        alert(data.name + " , " + data.value )
    };

Note:Don't forget to return $(this) object in order to mantain chain ability of jQuery

Just try the following pattern:

    $.fn.myplugin = function(options) {
        var settings = $.extend({}, options);

        alert(settings.name + " , " + settings.value);
    };
发布评论

评论列表(0)

  1. 暂无评论