我試圖在加載頁面時隱藏一些元素。隱藏特定元素onload或ready或page:如果選擇標記具有特定值,則更改
目前我使用的是'page:change'
,因爲我在使用Turbolinks的Ruby on Rails中工作。
如果div包含具有特定值的選擇標記,我想隱藏一些元素。
該頁面可以包含多個這些選擇標記,因此我無法使用ID。這裏
代碼不起作用:
$(document).on('page:change', function(){
if ($('.q_type_select').val() == 'text' || $('.q_type_select').val() == 'area') {
$(this).closest('.question_fieldset').children('.choice_fieldset').hide();
$(this).closest('.question_fieldset').children('.add_fields').hide();
};
};
我想如果你單擊$(this)
僅適用?
如果我這樣做:
$(document).on('page:change', function(){
if ($('.q_type_select').val() == 'text' || $('.q_type_select').val() == 'area') {
$('.question_fieldset').children('.choice_fieldset').hide();
$('.question_fieldset').children('.add_fields').hide();
};
};
我得到所有具有該類隱藏,而不是我想要的那些元素。
有什麼建議嗎?
只需使用$(文件)。就緒[導軌4 –
可能的複製:如何使用$(document).ready()與turbo鏈接](http://stackoverflow.com/questions/18770517/rails-4-how-to-use-document-ready-with-turbo-links) –
這是如何回答這個問題的?我沒有問這個問題,請閱讀這個問題。 – aleherzko