隐藏特定元素onload或ready或page:如果select标签具有特定值,则更改(Hide specific elements onload or ready or page:change if

系统教程 行业动态 更新时间:2024-06-14 16:59:47
隐藏特定元素onload或ready或page:如果select标签具有特定值,则更改(Hide specific elements onload or ready or page:change if select tags have specific value)

我正在尝试在加载页面时隐藏一些元素。

目前我正在使用'page:change'因为我正在使用Turbolinks在Ruby on Rails上工作。

如果div包含具有特定值的select标记,我想隐藏一些元素。 该页面可以包含多个选择标签,因此我无法使用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(); }; };

我得到了隐藏该类的所有元素而不是我想要的那些元素。

有什么建议么?

I'm trying to hide some elements when a page is loaded.

Currently I'm using 'page:change' because I'm working in Ruby on Rails with Turbolinks.

If the div contains a select tag with a specific value, I want to hide some elements. The page can contain multiple of those select tags, so I can't use ID's.

Code here does not work:

$(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(); }; };

I guess that $(this) only works if you click?

If I do 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(); }; };

I get all of the elements that have that class to hide and not the ones I want.

Any suggestions?

最满意答案

假设你有什么是正确的(因为没有HTML可供查看)。 尝试这个

$(function(){ $('.q_type_select').each(function(){ var $this = $(this); if ($this.val() == 'text' || $this.val() == 'area') { $this.closest('.question_fieldset').children('.choice_fieldset').hide(); $this.closest('.question_fieldset').children('.add_fields').hide(); }; }) });

循环遍历文档就绪函数页面中存在的所有元素( .q_type_select )并隐藏()它

Assuming whatever you have there is correct (since there is no HTML to look to). Try this

$(function(){ $('.q_type_select').each(function(){ var $this = $(this); if ($this.val() == 'text' || $this.val() == 'area') { $this.closest('.question_fieldset').children('.choice_fieldset').hide(); $this.closest('.question_fieldset').children('.add_fields').hide(); }; }) });

Loop through all of the element (.q_type_select) present in the page on document ready function and hide() it

更多推荐

本文发布于:2023-04-17 09:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/6bd70673ded544440c26cd55a97ca97d.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:则更   元素   定值   标签   page

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!