我剛創建了一個selectbox
plugin.It非常簡單,我試圖修改它。jquery插件綁定更改事件
其實它在做的是隱藏選擇並在select
之後添加一些ul,li
。
說,我可
<select id="test">
<option>1</option>
<option>2</option>
</select>
現在,這些選項我可以做
$("#test").selectBox(); //to make it my selectBox
我有setter和getter選項
$("#test").selectBox("changeValue",["changed option",index]);
在我的插件,我使用的方法。 。
var methods = {
init :function(options){},
changeValue:function(value){
//some code to change the value ...
}
};
我可以改變這個值,但是我怎樣才能捕捉到change
事件?
change
意味着,在選擇或改變ul,li
我真正需要的是
$("#test").selectBox(); //initialise
$("#test").change(function(){ //attach change event
//my codes.....
});
或
$("#test").selectBox({
onChange : function(){
console.log("changed....");
}
});
希望這個問題是清楚了。 謝謝。
編輯:這裏是FIDDLE鏈接http://jsfiddle.net/jitheshkt/rBjqq/3/ 請注意,我不是這方面的專家,只是試圖做到這一點。
* 編輯:我在每個函數裏寫了一些事件,所以我認爲那會是問題。我改變了它,並運行良好*
看看['.trigger()'](http://api.jquery.com/trigger/)。您可以在'changeValue'方法中的匹配元素上觸發一個事件,並在該元素上偵聽它。 – 2012-07-24 08:47:19
@JamesAllardice謝謝..看起來很有前途的......但是請你幫助我多一點嗎? – Red 2012-07-24 08:57:57
你能否顯示你的初始化代碼正在做什麼,以原始選擇元素。 – 2012-07-24 09:00:13