1
我想建立一個鏈式選擇菜單。這是我用的教程:http://railscasts.com/episodes/88-dynamic-select-menus 出錯了,我不知道我錯過了什麼。動態菜單選擇導軌不工作
的JavaScript
var sottocategorie = new Array();
<% for element in @sottocategorie -%>
sottocategorie.push(new Array(<%= element.idcategoria %>, <%= element.c2 %>));
<% end -%>
function selezionacategoria() {
categoriaid = $('segnalazione_categoria1').getValue();
options = $('segnalazione_categoria2').options;
options.length = 1;
sottocategorie.each(function(elementement) {
if (element[0] == categoriaid) {
options[options.length] = new Option(element[1]);
}
});
if (option.length == 1) {
$('sottocategoria_field').hide();
} else {
$('sottocategoria_field').show();
}
}
document.observe('dom:loaded', function() {
//selezionacategoria();
$('segnalazione_categoria1').observe('change', selezionacategoria);
});
HTML
<label for="segnalazione_categoria1">Categoria:</label>
<%= f.collection_select :categoria1, Categorium.find(:all), :id, :c1, :prompt => "Seleziona categoria" %>
<p id="sottocategoria_field">
<label for="segnalazione_categoria2">Categoria:</label>
<%= f.collection_select :categoria2, Sottocategoria1.find(:all), :id, :c2, :prompt => "Seleziona sottocategoria" %>
</p>
路線:
match '/:controller(/:action(/:id))'
鏈式選擇菜單不運行,「過濾器」不起作用,並且也是
if (option.length == 1) {
$('sottocategoria_field').hide();
} else {
$('sottocategoria_field').show();
}
不起作用。
其軌道的版本,您使用的?哪個js庫原型js或jQuery? – denisjacquemin 2011-06-13 15:34:56
Rails 3.0.5,我正在使用jQuery 1.6.1 – 2011-06-13 15:44:41
好吧... railscasts(漂亮的老)使用prototypejs代碼,並使用jquery ... – denisjacquemin 2011-06-13 15:46:52