使用jQuery仅处理文本输入字段

时间:2016-10-28 10:24:09

标签: jquery labels

您好我正在尝试仅处理文本字段,以便我可以仅为那些隐藏标签。 我仍然希望复选框和readio按钮的标签可见。 隐藏的代码可以工作,但它隐藏了所有标签。

该脚本必须适用于所有情况,无论字段是什么或如何命名。 那么我如何才能解决文本输入字段的标签:

$("label").each(function () {

我似乎无法解决这个问题。

这是html的示例:

<p class="form-field pd-text">
 <label class="field-label" for="229122">E-post</label>
 <input type="text" name="229122" id="229122" value="" class="text" size="30" maxlength="255" />
</p>
<p class="form-field pd-checkbox">
 <label class="field-label" for="229122">Example checkbox</label>
 <span class="value"><span>
  <input type="checkbox" name="229122" id="229122" value="25034" onchange="" />
  <label class="inline" for="229122">false</label>
 </span></span>
</p>

4 个答案:

答案 0 :(得分:1)

试试这个 $('[type=text]').prev().hide();

如果在任何情况下你在输入之前移动你的标签到两级后退或前进。 你可以像那样的目标

 var labelFor = $('[type=text]').attr('id');
 $("label[for="+labelFor+"]").hide()

答案 1 :(得分:0)

试试这个。

&#13;
&#13;
$("input[type='text']").prev('label').hide();
&#13;
<p class="form-field pd-text">
 <label class="field-label" for="229122">E-post</label>
 <input type="text" name="229122" id="229122" value="" class="text" size="30" maxlength="255" />
</p>
<p class="form-field pd-checkbox">
 <label class="field-label" for="229122">Example checkbox</label>
 <span class="value"><span>
  <input type="checkbox" name="229122" id="229122" value="25034" onchange="" />
  <label class="inline" for="229122">false</label>
 </span></span>
</p><script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可以使用(numeric_python) luca@luca-VirtualBox:~/Documents/datasets/RNDC/7days$ zcat out_batch_1_start_1476896400_end_1476900000_.gz | head Protocol,Src,Src Port,Dst,Dst Port,Group ID,Port,VPort,IP TOS,VLAN ID,VLAN Pri,MPLS Exp,Application,Packets,Messages,Bytes,First,Last,"EmergingThreats category","EmergingThreats description","EmergingThreats detailedDescription","EmergingThreats first_seen","EmergingThreats last_seen","EmergingThreats ports","EmergingThreats score","Entity AD_SecurityGroup","Entity Privileged","Entity Unique_Users","Entity User","OS OS","STIX description","STIX detailedDescription","STIX timeStamp","iSIGHT ThreatScape","iSIGHT asn","iSIGHT domain","iSIGHT networkIdentifier","iSIGHT networkType","iSIGHT port","iSIGHT productType","iSIGHT publishDate","iSIGHT reportId","iSIGHT reportLink","iSIGHT title","iSIGHT webLink" ETH:0x0000,00:17:A4:77:9C:04,,09:00:2B:00:00:05,,2,PortC,0,,,,,"Unknown",472,0,28320,1476896408,1476899995,,,,,,,,,,,,,,,,,,,,,,,,,,, ETH:0x0000,00:17:A4:77:9C:0A,,09:00:2B:00:00:04,,2,PortC,0,,,,,"Unknown",1938,0,125769,1476899771,1476899999,,,,,,,,,,,,,,,,,,,,,,,,,,, ETH:0x0000,00:17:A4:77:9C:0A,,09:00:2B:00:00:05,,2,PortC,0,,,,,"Unknown",498,0,29880,1476896400,1476899999,,,,,,,,,,,,,,,,,,,,,,,,,,, ETH:0x0000,00:26:F2:C1:00:00,,01:80:C2:00:00:00,,2,PortC,0,,,,,"Unknown",1801,0,108060,1476896401,1476899999,,,,,,,,,,,,,,,,,,,,,,,,,,, ETH:0x0000,00:26:F2:C6:00:00,,01:80:C2:00:00:00,,2,PortC,0,,,,,"Unknown",1801,0,108060,1476896401,1476899999,,,,,,,,,,,,,,,,,,,,,,,,,,, ETH:0x0000,00:26:F3:2A:00:00,,01:80:C2:00:00:00,,2,PortC,0,,,,,"Unknown",1801,0,108060,1476896401,1476899999,,,,,,,,,,,,,,,,,,,,,,,,,,, ETH:0x0000,00:26:F3:8C:00:00,,01:80:C2:00:00:00,,2,PortC,0,,,,,"Unknown",1801,0,108060,1476896401,1476899999,,,,,,,,,,,,,,,,,,,,,,,,,,, ETH:0x0000,00:30:6E:48:17:18,,09:00:09:00:00:01,,2,PortC,0,,,,,"Unknown",13,0,780,1476896412,1476899999,,,,,,,,,,,,,,,,,,,,,,,,,,, ETH:0x0000,84:78:AC:15:44:BC,,01:80:C2:00:00:00,,2,PortC,0,,,,,"Unknown",1801,0,108060,1476896401,1476899999,,,,,,,,,,,,,,,,,,,,,,,,,,, (numeric_python) luca@luca-VirtualBox:~/Documents/datasets/RNDC/7days$ 根据.map属性找到匹配的标签:

for=

示例小提琴:https://jsfiddle.net/rdtt8toe/

注意这要求var labels = $("input[type='text']").map(function() { return $("label[for=" + this.id + "]").toArray() }) labels.hide(); 是唯一的,它必须是(并且在小提琴中)但在代码中不是。因此,使用您的html,将找到所有标签,因为复选框具有与文本框相同的ID。

或者,您可以对DOM层次结构进行假设。例如:

id=

查找同一容器中的所有标签。这将在前面和后面找到它们,并且可以在其间具有其他元素(与$("input[type='text']").parent().find("label") 不同!)

在您的情况下,您可能更愿意转到最近的.prev

.form-field

答案 3 :(得分:0)

您可以为文本输入标签提供自定义类

<p class="form-field pd-text">
    <label class="field-label text-label" for="229122">E-post</label>
    <input type="text" name="229122" id="229122" value="" class="text" size="30" maxlength="255" />
</p>
<p class="form-field pd-checkbox">
    <label class="field-label" for="229122">Example checkbox</label>
    <span class="value"><span>
        <input type="checkbox" name="229122" id="229122" value="25034" onchange="" />
        <label class="inline" for="229122">false</label>
    </span></span>
</p>

然后只隐藏那些标签

$(".text-label").hide()

如果您要隐藏所有表单字段,请将自定义类指定给该字段

即:

<p class="form-field pd-text text-label">
    <label class="field-label" for="229122">E-post</label>
    <input type="text" name="229122" id="229122" value="" class="text" size="30" maxlength="255" />
</p>