动态更新原型 selected.js

2024-01-15

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

它说要动态更新 selected.js,我们应该使用这个片段

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 版本以更新?


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

尝试这个:

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

    Event.fire(nev, 'liszt:updated');
});

UPDATE

好吧,经过彻底调查,我发现了问题所在,event.simulate没有被包含在内,我完全修复了代码,请查看这把小提琴 http://jsfiddle.net/epoch/ckA4k/.

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

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');             
    }
}
​
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

动态更新原型 selected.js 的相关文章