1

シンプルな jquery プラグインを作成しようとしています。ここに私がこれまでに得たもののリンクがあります

http://jsfiddle.net/T4G96/

(function ($) {

    $.bsDatepicker= function(element) {

        var $element=$(element);

        $element.attr("readonly","readonly");


        $element.on("mousedown",function(){
            renderCalendar();

        });




    renderCalendar = function(){
        alert($element.attr("name"));

    }

  }

     $.fn.bsDatepicker = function(options) {

        return this.each(function() {
            $abc=$(this);
            // if element has a date picker already attached
            if (undefined != $(this).data('bsDatepicker')) {

                // get reference to the previously attached date picker
                var plugin = $(this).data('bsDatepicker');

                // remove the attached icon (if it exists)...


            }

            // create a new instance of the plugin
            var plugin = new $.bsDatepicker(this, options);

            // save a reference to the newly created object
            $(this).data('bsDatepicker', plugin);

        });

    }

})(jQuery)

$(document).ready(function(){
    $("#cal").bsDatepicker();
    $("#cal2").bsDatepicker();
});

私が直面している問題は、このプラグインを異なる名前と ID を持つ 2 つのテキスト ボックスに実装することです。mousedown イベントでコンソールに要素名を出力しました。ここでの問題は、常に 2 番目のテキスト ボックス名を出力することです。クリックされている要素の名前を出力する必要があります。ここで何が間違っていますか?どんな助けでも大歓迎です。

ありがとう

4

1 に答える 1

0

renderCalendar()変数から読み取ろうとするのではなく、イベントをトリガーした要素の参照を関数に渡す必要があると思います

$element.on("mousedown",function(){
    renderCalendar(this);       
});

renderCalendar = function(element){
    alert($(element).attr("name")); 
}

フィドル

于 2013-07-22T06:30:00.863 に答える