Javascript 动态更新prototype Selected.js
我是一个新的原型,我不能真正理解这里提供的极简主义文档 它说要动态更新selected.js,我们应该使用这个代码段Javascript 动态更新prototype Selected.js,javascript,prototypejs,dom-events,prototype-chosen,Javascript,Prototypejs,Dom Events,Prototype Chosen,我是一个新的原型,我不能真正理解这里提供的极简主义文档 它说要动态更新selected.js,我们应该使用这个代码段 Event.fire($("form_field"), "liszt:updated"); 我不明白的是,该事件.fire需要针对哪个元素。在我的例子中,我有一个带有两个select元素的动态表单。只有在用户选择第一个选择元素上的选项后,才启用第二个选择元素。如下图所示: 所以我只是在我的代码上试了一下。这是: // let say I want to make all .c
Event.fire($("form_field"), "liszt:updated");
我不明白的是,该事件.fire需要针对哪个元素。在我的例子中,我有一个带有两个select元素的动态表单。只有在用户选择第一个选择元素上的选项后,才启用第二个选择元素。如下图所示:
所以我只是在我的代码上试了一下。这是:
// 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");
});
//假设我想将all.chzn select元素替换为selected.js
var el=$$(“.chzn选择”);
//这是将all.chzn select替换为selected.js的代码
document.observe('dom:loaded',函数(evt){
变量选择,选择,_i,_len,_结果;
if(Prototype.Browser.IE&&(Prototype.BrowserFeatures['Version']==6 | | | Prototype.BrowserFeatures['Version']==7)){return;}
选择=el;_结果=[];
对于(_i=0,_len=selects.length;_i<_len;_i++){
选择=选择[_i];
_结果:推送(新选择(选择));
}
});
//这是我用来观察.chzn select元素的更改事件的
el.invoke('observe','change',function(){
//在更改其他一些.chnz select之后,我已成功更新了所有.chzn select元素
肌功能;
//这就是我感到困惑的地方,我想更新所有生成的Selected.js选择器
事件。火灾(el,“李斯特:更新”);
});
在那个例子中,Event.fire似乎根本不起作用。。。那么我做错了什么?我如何才能在用户选择大小选择后将选定的.js版本的color select更新为update?在调用所有元素上的观察事件时。chzn select
元素,使用el
作为当前元素,它不会反映数组中的实际元素
试试这个:
el.invoke('observe', 'change', function(ev) {
var nev = Event.element(ev);
myOwnObject.myOwnFunction(nev);
Event.fire(nev, 'liszt:updated');
});
更新
好的,在彻底调查之后,我发现了问题,event.simulate
没有包含在内,我完全修复了代码,请检查
以下是小提琴中使用的代码:
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');
}
}
谢谢你。。。我试过了,但什么都没发生(它仍然像以前一样…myown函数工作…Event.fire no…你知道还有什么可能是错误的吗?我检查了firebug控制台,它显示了这个错误:节点未定义。element()事件=[选择#attribute525.required-entry,选择#attribute272.required entry]