【问题标题】:Update prototype chosen.js dynamically动态更新原型 selected.js
【发布时间】:2012-06-25 20:43:09
【问题描述】:

我是 Prototype 的新手,我无法真正理解此处提供的极简主义文档 http://harvesthq.github.com/chosen/

它说要动态更新choice.js,我们应该使用这个sn-p

Event.fire($("form_field"), "liszt:updated");

我不明白的是,哪个元素需要针对那个Event.fire。在我的例子中,我有一个带有两个选择元素的动态表单。只有在用户选择第一个选择元素上的选项后,才会启用第二个选择元素。就像这个插图:

所以我只是在我的代码上进行了尝试。这里是:

// let say I want to make all .chzn-select element replaced by chosen.js
var el = $$(".chzn-select");

// this is the code to make all .chzn-select replaced by chosen.js
document.observe('dom:loaded', function(evt) {
     var select, selects, _i, _len, _results;
     if (Prototype.Browser.IE && (Prototype.BrowserFeatures['Version'] === 6 || Prototype.BrowserFeatures['Version'] === 7)) { return; }
     selects = el;  _results = [];
     for (_i = 0, _len = selects.length; _i < _len; _i++) {
        select = selects[_i];
        _results.push(new Chosen(select));
     }
}); 

// this is what I used to observe the change event of the .chzn-select element
el.invoke('observe', 'change', function() {
    // I have successfully updated all the .chzn-select element after the change of some other .chnz-select
    myOwnObjet.myOwnFunction(el);

    // now this is where I get confused, I want to update all the generated chosen.js selector
    Event.fire(el, "liszt:updated");
});

在那个例子中,那个 Event.fire 似乎根本不起作用......那么我在这里做错了什么?在用户选择尺寸选择后,我如何才能更新该 selected.js 版本的颜色选择以更新?

【问题讨论】:

    标签: javascript prototypejs dom-events prototype-chosen


    【解决方案1】:

    在您对所有.chzn-select 元素调用observe 事件时,您使用el 作为当前元素,它不会反映数组中的实际元素。

    试试这个:

    el.invoke('observe', 'change', function(ev) {
        var nev = Event.element(ev);
        myOwnObject.myOwnFunction(nev);
    
        Event.fire(nev, 'liszt:updated');
    });
    

    更新

    好的,经过深入调查,我发现了问题,event.simulate没有被包含,我完全修复了代码,查看this fiddle

    这是小提琴中使用的代码:

    var selects;
    document.observe('dom:loaded', function(evt) {
        selects = $$('select.chzn-select').inject($H(), function(hsh, sl) {
            hsh.set(sl.id, new Chosen(sl));
            return hsh;
        });
    
        selects.each(function(pair) {
            Event.observe($(pair.key), 'change', function(ev) {
                myOwnFunction($(pair.key), pair.value);
            });
        });
    });
    
    function myOwnFunction(select, chzn) {
        if (select.id === 'sl-color') {
            var size = $('sl-size');
            size.disabled = false;
    
            Event.fire(size, 'liszt:updated');             
        }
    }
    ​
    

    【讨论】:

    • 谢谢时代...我试过了,但什么也没发生... :(它仍然像以前一样工作... myOwnFunction 工作... Event.fire 不...你有什么想法还有什么可能是错的
    • 我检查了萤火虫控制台,它显示了这个错误:节点未定义。 element() 事件 = [select#attribute525.required-entry, select#attribute272.required-entry]
    猜你喜欢
    • 1970-01-01
    • 2018-12-05
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多