在YUI中的onChange

4
如何在YUI2和YUI3中编写onChange函数的代码。
jQuery(':input[type=radio]').change(function(){

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

});

1
这可能会有所帮助:https://dev59.com/kXM_5IYBdhLWcg3w02v8 - Justin Ethier
1个回答

9
在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');
    ...
});

在YUI 2.9中(该版本已不再维护;请使用YUI 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;
    ...
});

谢谢!但是当我在JSFiddle中尝试时,它会弹出值1、2的警告。这里是URL http://jsfiddle.net/wasim117/cEYBv/。 - Wasim Shaikh
抱歉!我打破了我的规则,从未使用nodelist.on() --委托更好。你得到两个值的原因是nodelist订阅中的“this”是nodelist,而不是接收事件的单个节点。然而,e.currentTarget是该节点。我已更新代码片段。 - Luke
谢谢,我目前使用的是2.9版本,你的解决方案很有效!顺便说一句,你也可以使用.addListener(..)代替.on(..)。 - Kostanos
@Luke 对于 YUI 2.9,我认为 Event.on 无法捕获后来动态添加到表单中的元素,而 delegate 在 IE7 中使用 change 时存在问题,你有其他解决方案吗?我尝试了使用 delegateblur,但似乎不被支持。 - Mehdi Karamosly

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接