hide() 单选按钮 * 和 * 它在 jquery 中的文本标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/643704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:30:10  来源:igfitidea点击:

hide() radio button *and* its text label in jquery

jqueryradio-button

提问by edzillion

I am going back over a recent project sorting out accessibility issues and was making sure all form elements had labels. Putting the label text into a tag caused a problem with some kludgy code I had written before.

我正在回顾最近的一个项目,解决可访问性问题,并确保所有表单元素都有标签。将标签文本放入标签会导致我之前编写的一些乱七八糟的代码出现问题。

Basically, if you have a radio button and its label:

基本上,如果您有一个单选按钮及其标签:

<label for="zone_r1"><input type="radio" name="zone" id="zone_r1" value="NY" />New York</label>

And you use jquery to hide it like so:

你使用 jquery 来隐藏它,如下所示:

$('#zone_r1').hide();

The actual button is hidden but not the label text. Originally I made a span for the label text and hid that like so:

实际的按钮是隐藏的,但不是标签文本。最初我为标签文本制作了一个跨度并将其隐藏起来:

<input id="NY" type="radio" name="zone" value="NY" /><span id="nyTXT">New York</span>

and

$('#NY').hide();
$('#nyTXT').hide();

Any ideas? I prefer not to use the kludge and it may not validate with the span in the label, but maybe I am being overly zealous.

有任何想法吗?我不喜欢使用 kludge,它可能无法通过标签中的跨度进行验证,但也许我过于热心了。

回答by ckramer

I think this should work for you

我认为这应该适合你

$("label[for=zone_r1],#zone_r1").hide();

This selects the label with the "for" attribute set to the radio button your looking for, as well as the radio button itself, and hides them both

这将选择“for”属性设置为您要查找的单选按钮的标签以及单选按钮本身,并将它们都隐藏起来

回答by cobbal

$('#zone_r1').parent().hide();

works for me

为我工作

回答by Kobi

what about $('label:has(#zone_r1)').hide();

关于什么 $('label:has(#zone_r1)').hide();

回答by CMS

For the first radio button, you can hide the actual button and then its parent:

对于第一个单选按钮,您可以隐藏实际按钮,然后隐藏其父按钮:

$('#zone_r1').hide().parent().hide();

For the second case you can hide the button and the "next" sibling:

对于第二种情况,您可以隐藏按钮和“下一个”兄弟:

$('#NY').hide().next().hide();

回答by psuphish05

You can do this:

你可以这样做:

1.) Define the radio button input without a surrounding label.

1.) 定义没有周围标签的单选按钮输入。

2.) Wrap the "option text" (text to the right of the radio button) in a <span>.

2.) 将“选项文本”(单选按钮右侧的文本)包裹在<span>.

3.) Use this jQuery statement: $("input:radio:not(:checked), input:radio:not(:checked) + span").hide();

3.) 使用这个 jQuery 语句: $("input:radio:not(:checked), input:radio:not(:checked) + span").hide();

This will select the radio button and the text to the right of the radio button and hide it.

这将选择单选按钮和单选按钮右侧的文本并将其隐藏。

回答by Denis Yusupov

Just put 'label' selector in parent:

只需将“标签”选择器放在父级中:

$(':radio[id=zone_r1]').parent('label').hide();

See my jsFiddle example

我的 jsFiddle 例子

回答by ShahidAliK

hiding radio button container or TD based on value

根据值隐藏单选按钮容器或 TD

jQuery("input[type=radio][value='EU2']").parent().hide();

jQuery("input[type=radio][value='EU2']").parent().hide();