jQueryのプラグイン作り

2010/04/28 追記
この記事で作ったjQueryのNumericUpDownプラグインを公開しました。
http://d.hatena.ne.jp/mkt23/20100427/1272351878


前のエントリー書いてから1時間経っておりませんが...
 
適当にやってみたら出来ました!
 
というわけでまとめ。

                                                                                                              • -

プラグイン、オプションの所に valChange:function(){} を追加。

//option
opt = $.extend({
  UpButtonImage: "/lib/image/BGupbutton.png",
  DownButtonImage: "/lib/image/BGdownbutton.png",
  hoverPosition: "0 -12px",
  activePosition: "0 -24px",
  max: null,
  min: null,
  interval: 1,
  timeInterval: 250,
  valChange:function(){}
}, opt || {});


そして、プラグインの中で↓の用にイベントにバインドする。

var txtB = $(this); // this はINPUT(text)
txtB.bind("change", opt.valChange);

あとは、プラグインを使うjavascriptの中で動作を設定。

$(".spinbox").NumericUpDown({max:50, min:0,
  valChange:function(){
    alert("値が変わった!");
  }
});

 
思ったより簡単だった。
 
実は昨年中もどうやるのか考えてたんだけど、
jQuery UI のコードを覗き見して混乱して投げ出してた!
 
案ずるより産むが易しです。