2016-09-27 132 views
-1

我是新來的Javascript和您的建議和幫助將不勝感激。如何在頁面刷新時使用javascript顯示保存的字段 - JavaScript Rails4

    當選項 "only men""only women"被選擇場 "quantity"顯示
  • 當選項"both men & women"選擇字段顯示"quantity_men" & "quantity_women"
  • 當我選擇選項"both men & women"和字段"quantity_men" & "quantity_women"顯示,當我寫我的qunatity和保存並刷新頁面的字段被隱藏

問題

  • 的男性&婦女得救了,我如何避免當我刷新頁面被隱藏的class= specific_gender_quantity_content數量。
  • 我嘗試下面的代碼,但我不確定,因爲我是新和你的幫助會非常讚賞

的意見/事件/ _form.html.erb

<%= f.association :category_quantitygender, collection: CategoryQuantitygender.all, prompt: "select a category", label: "Quantity Gender Selection" %> 
    <div class="general_gender_quantity_content"> 
     <%= f.input :quantity, placeholder: "quantity", label: false %> 
    </div> 
    <div class="specific_gender_quantity_content"> 
     <%= f.input :quantity_men, placeholder: "quantity men", label: "men" %> 
     <%= f.input :quantity_women, placeholder: "quantity women", label: "women" %> 
    </div> 

資產/ JavaScript的/ events_form .js文件

$(document).ready(function() { 
    $('.general_gender_quantity_content').hide(); 
    $('.specific_gender_quantity_content').hide(); 
    $('#social_category_quantitygender_id').change(function() { 
    if ($(this).val() == 3) { 
     $('.specific_gender_quantity_content').show(); 
     $('.general_gender_quantity_content').hide(); 
    } else if ($(this).val() == 1, 2) { 
     $('.general_gender_quantity_content').show(); 
     $('.specific_gender_quantity_content').hide(); 
    } 
    }); 
}); 

enter image description here

回答

-1

您可以獲取所選值,然後根據所選值顯示隱藏div。

$(document).ready(function() { 
var social_category_quantitygender_id = $('#social_category_quantitygender_id').val(); 
if (social_category_quantitygender_id == 3) { 
    $('.specific_gender_quantity_content').show(); 
} else if (social_category_quantitygender_id == 1, 2) { 
    $('.general_gender_quantity_content').show(); 
} else { 
    $('.general_gender_quantity_content').hide(); 
    $('.specific_gender_quantity_content').hide(); 
} 

});