2011-08-12 35 views
4

如何在YUI 2和YUI3中編寫onChange函數的代碼。onChange in YUI

jQuery(':input[type=radio]').change(function(){ 

    var aType=jQuery(this).val(); 
    var numT= aType==1 ? "3" : "6" ; 
    var aWidth=aType==1 ? "330px" : "660px" ; 

}); 
+1

這可有助於:http://stackoverflow.com/questions/1215012/yui-3-programmatically-fire-change-event –

回答

8

YUI 3

Y.all('input[type=radio]').each(function (node) { 
    node.on('change', function() { 
     var val = this.get('value'); 
     ... 
    }); 
}); 

// or 
Y.all('input[type=radio]').on('change', function (e) { 
    var val = e.currentTarget.get('value'); 
    ... 
}); 

在銳2.9(後者不再正在積極開發;使用銳3)

// typical implementations alias Event or Event.on and 
// Selector.query to shorter names 
YAHOO.util.Event.on(
    YAHOO.util.Selector.query('input[type=radio]'), 'change', function (e) { 
     var val = this.value; 
    ... 
}); 
+0

謝謝!但是當我在JSFiddle中嘗試這種方法時,它的alert值都爲1,2,這裏是URL http://jsfiddle.net/wasim117/cEYBv/ –

+0

對不起!我打破了我從不使用nodelist.on()的規則 - 放棄更好。您獲得這兩個值的原因是,nodelist訂閱中的「this」是節點列表,而不是收到該事件的單個節點。不過,e.currentTarget是節點。我更新了代碼片段。 – Luke

+0

謝謝你,我現在用的是2.9,你的解決方案效果很好! BTW。你也可以使用.addListener(..)而不是.on(..) – Kostanos