简体   繁体   English

隐藏与其关联的文本框字段和标签

[英]Hide Textbox Field and Label associated with it

This is probably a pretty pointless question but I reckon it would be useful for many of us. 这可能是一个毫无意义的问题,但我认为这对我们很多人都有用。

In this case's scenario, I would like to be able to hide a textbox and its label all in one go, without having to select both element and hiding them individually. 在这种情况下,我希望能够一次性隐藏文本框及其标签,而无需选择两个元素并单独隐藏它们。

<label for="a">some text:</label>
<input type="text" class="text" id="a" />

So, for the above case, would it be possible to use a simple jQuery command to hide them both together? 那么,对于上面的情况,是否可以使用简单的jQuery命令将它们隐藏在一起?

You could do this 你可以做到这一点

$('label[for=a], input#a').hide();

http://jsfiddle.net/jasongennaro/dYFMU/ http://jsfiddle.net/jasongennaro/dYFMU/

[ ] selects the attribute . [ ]选择attribute In this case we are targeting a for attribute that equals a . 在这种情况下,我们的目标是一for ,等于属性a

At the same time, we use a , to make another selection, the input with an id=a . 与此同时,我们使用,进行另一选择中, inputid=a

EDIT 编辑

Also, if you needed to do this for multiple labels and inputs you could put the label and id in an array as follows: 此外,如果您需要为多个labelsinputs执行此操作,您可以将labelid放在数组中,如下所示:

<label for="a">some text:</label>
<input type="text" class="text" id="a" />

<br />

<label for="b">some text:</label>
<input type="text" class="text" id="b" />

<br />

<label for="c">some text:</label>
<input type="text" class="text" id="c" />

js JS

var a = ['a','b','c'];

for(var i=0; i<a.length; i++){
    $('label[for=' + a[i] + '], input#' + a[i]).hide();
}

http://jsfiddle.net/jasongennaro/dYFMU/1/ http://jsfiddle.net/jasongennaro/dYFMU/1/

if you want to reuse the hiding, you can create a function similar to: 如果要重用隐藏,可以创建类似于以下的函数:

function hideTandem(id){
    $('#' + id + ', label[for=' + id + ']').hide();
}

and call it by supplying the id of the input 并通过提供输入的id来调用它

hideTandem('a');

The simplest way would probably be to just wrap them in a div and hide that instead. 最简单的方法可能是将它们包装在div并隐藏它。

<div id="hidethis">
  <label for="a">some text:</label>
  <input type="text" class="text" id="a" />
<div>

$("#hidethis).hide();

Find the input however you want, and then you can find its ID using 找到您想要的输入,然后您可以使用找到它的ID

var myInputBoxID = $(some selector).attr("id");

You can then reference the label by using the following syntax to catch the label's for attribute: 然后,您可以使用以下语法来引用标签以捕获属性的标签:

$("label[for=' + myInputBoxID + ']").hide();

You can probably nest things nicely so you don't need to store anything as a variable, and instead run through a "for each" on all the results of your input box selector. 您可以很好地嵌套事物,因此您不需要将任何内容存储为变量,而是在输入框选择器的所有结果上运行“for each”。 But anyway the key is finding the ID and then using that to find the label with label[for=...] 但无论如何关键是找到ID然后用它来找到带有标签的标签[for = ...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM