jQuery 获取输入字段的标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18375457/
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
Get label for input field
提问by Kim
I'm doing a validation with Jquery and need to get the $label from each element with their own label. Now the alert() gives med [object object]. The best thing for me here is to get an alert() with all fields lined up that is not filled out. And not an alert() for each.
我正在使用 Jquery 进行验证,需要从每个元素中获取带有自己标签的 $label。现在 alert() 给出了 med [object object]。对我来说,这里最好的事情是得到一个 alert(),其中所有未填写的字段都排成一行。而不是每个的 alert() 。
Here is a fiddle: http://jsfiddle.net/s7pYX/
这是一个小提琴:http: //jsfiddle.net/s7pYX/
How is this accomplished?
这是如何实现的?
HTML:
HTML:
<div>
<label for="txtName">Name</label>
<input type="text" id="txtName" class="form-control" name="txtName">
</div>
<div>
<label for="txtEmail">E-mail</label>
<input type="text" id="txtEmail" class="form-control" name="txtEmail">
</div>
Jquery:
查询:
$('input').each(function(){
if ($(this).val() == '') {
$element = $(this)
var $label = $("label[for='"+$element.attr('id')+"']")
alert($label)
}
});
In the alert() I expect like this "You need to fill out: Name, E-mail"
在警报()中,我希望像这样“您需要填写:姓名,电子邮件”
回答by Arun P Johny
Try to alert the contents of $label, you can use .text()for this
尝试提醒 的内容$label,您可以为此使用.text()
$('input').each(function(){
var $element = $(this)
if ($element.val() == '') {
var $label = $("label[for='"+this.id+"']")
alert($label.text())
}
});
Demo: Fiddle
演示:小提琴
Update
更新
var $labels = $("label[for]");
var empties = $('input').filter(function(){
return $.trim($(this).val()) == ''
}).map(function(){
return $labels.filter('[for="'+this.id+'"]').text()
}).get().join(', ')
alert(empties)
Demo: Fiddle
演示:小提琴
回答by Suresh Atta
Try
尝试
$('input').each(function(){
if ($(this).val() == '') {
$element = $(this)
var $label = $("label[for='"+$element.attr('id')+"']")
alert("You need to fill :" + $label.text())
}
});
Update :
更新 :
is it possible to arrange all the $label in one alert() and not one alert() each?
是否可以将所有 $label 安排在一个 alert() 而不是每个 alert() 中?
Yes
是的
var errorString ="";
var isNeedToFill=false;
$('input').each(function(){
if ($(this).val() == '') {
isNeedToFill =true;
$element = $(this)
var $label = $("label[for='"+$element.attr('id')+"']");
errorString += $label.text()+" ";
}
});
if(isNeedToFill){
alert("You need to fill :" +errorString);
}
回答by Kanishka Panamaldeniya
try to alert
试着提醒
alert($label.text())
回答by MaveRick
because you are sending an objectto the alert function, if you want to get the content of the selected label you have to get it's html
http://jsfiddle.net/s7pYX/2/
因为您正在向object警报功能发送一个,如果您想获取所选标签的内容,您必须获取它的 html
http://jsfiddle.net/s7pYX/2/
$('input').each(function(){
if ($(this).val() == '') {
$element = $(this)
var $label = $("label[for='"+$element.attr('id')+"']").html();
alert($label)
}
});
回答by Code L?ver
Instead of below line:
而不是下面的行:
var $label = $("label[for='"+$element.attr('id')+"']"
Use the following code:
使用以下代码:
var $label = $("label[for='"+$element.attr('id')+"']").text();
You are getting only the object you need to use the .html()or .text()to get the text inside the label tag.
您只获得使用.html()或.text()获取标签标签内的文本所需的对象。
回答by Arvind Bhardwaj
回答by Mark
This isn't a great example but you could use the prevfunction from jQuery which will find the previous label before your selected component.
这不是一个很好的例子,但您可以使用prevjQuery 中的函数,它会在您选择的组件之前找到上一个标签。
$(document).ready( function() {
$('input').each(function(){
alert($(this).prev("label").html())
});
});
JSFiddle: http://jsfiddle.net/gQnaM/
JSFiddle:http: //jsfiddle.net/gQnaM/
回答by Sheetal
var response = "";
$('input').each(function(){
if ($(this).val() == '') {
response += ", " + $('label[for="' + this.id + '"]').html();
}
});
alert(response.replace(", ", "You need to fill out: "));
回答by Catherine Audet
With JQuery, you can find all the labels linked to the input with $(input).labels();
使用 JQuery,您可以通过 $(input).labels(); 找到链接到输入的所有标签。
$('input').each( function() {
var $labels = $(this).labels();
$labels.each(function(index, element){
alert($(element));
});
});
回答by Andrew
Improving upon the answer you could wrap it in a function:
改进答案,您可以将其包装在一个函数中:
function getLabel(name) {
var $label = $("label[for='" + name + "']")
return ($label.text());
}
var label = getLabel(this.id);
console.log(label);
This makes it much easier to reuse the code.
这使得重用代码变得更加容易。

