나는 멀티 동적 선택 메뉴를 설계하고 있습니다. 즉, 브랜드 메뉴가 있습니다. 사용자가 JavaScript 및 Ajax를 사용하여 브랜드를 선택한 후 해당 브랜드에서 사용 가능한 모델을 검색하여 두 번째 선택 메뉴에 추가합니다. 이 프로세스는 다시 반복되지만 이번에는 선택된 모델의 기능을 보여줍니다.
이를 위해, 동일한 시스템이 필요한 여러 영역이 있기 때문에 모든 브랜드 선택 메뉴에서 동일한 이름을 가진 클래스를 사용하고 다른 하나는 모든 모델 선택 메뉴를 사용합니다.
<div class='brand_select' id='14'>
<%= f.collection_select :brand, Product.find_all_by_area(14, :group => 'brand'), :brand, :brand, :prompt => 'Choose brand' %>
</div>
<div class='model_select'>
<%= f.collection_select :model, Product.find_all_by_area(14), :model, :model, :prompt => 'Choose model' %>
</div>
<div class='brand_select' id='15'>
<%= f.collection_select :brand, Product.find_all_by_area(15, :group => 'brand'), :brand, :brand, :prompt => 'Choose brand' %>
</div>
<div class='model_select'>
<%= f.collection_select :model, Product.find_all_by_area(15), :model, :model, :prompt => 'Choose model' %>
</div>
그리고 Javacript :
$('.brand_select').change(function(event) {
// option selected
var brand=$(event.target).find('option:selected').val();
// if none is selected
if (brand == ''){
$(event.target).parent().parent().find('.modelo_select').hide();
$(event.target).parent().parent().find('.caracteristica').hide();
}
else {
$(event.target).parent().parent().find('.modelo_select').show();
// find id to search on the database
var id=$(event.target).parent().attr('id');
// find the target (id of the object)
var target=$(event.target).attr('id');
$.ajax({
type: "POST",
url: "http://"+location.host+"/model/"+brand+"/"+id+"/"+target,
brand: brand,
id: id,
target: target
});
}
});
$('.model_select').change(function(event) {
// find model selected to search on the database
var model=$(event.target).find('option:selected').val();
// find brand selected to search on the database
var brand=$(event.target).parent().parent().find('.marca_select').find('option:selected').val();
// find id to search on the database
var id=$(event.target).parent().parent().find('.marca_select').attr('id');
// find the target (id of the object)
var target=$(event.target).attr('id');
$.ajax({
type: "POST",
url: "http://"+location.host+"/feature/"+brand+"/"+model+"/"+id+"/"+target,
brand: brand,
model: model,
id: id,
target: target
});
});
이 코드는 작동하지만 이벤트는 해당 이름의 클래스와 동일한 횟수로 변경됩니다.
내가하고 싶은 것은 기능이 클래스에 대한 변경 이벤트가 요구 될 때마다 한 번만 실행하는 것입니다.
이것이 내가 가지고있는 클래스 구조로 이것이 가능하거나 각 영역에 대해 다른 이름을 가진 ID 또는 클래스를 기능에 연결 해야하는지 모르겠습니다.