使用 jQuery 查找选定控件或文本框的标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7455049/
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 23:52:23  来源:igfitidea点击:

Use jQuery to find the label for a selected control or textbox

jqueryjquery-selectors

提问by Darren

I want a bit of jQuery code that will allow me to find the label for a control when I click on the textbox... so in my HTML I have this:

我想要一些 jQuery 代码,当我点击文本框时,它可以让我找到控件的标签......所以在我的 HTML 中我有这个:

<label id="ctl00_WebFormBody_lblProductMarkup"  for="ctl00_WebFormBody_txtPriceAdjustment">This Is My Label Value</label>

<input type="text" style="width:29px;" onclick="alert('label value here');" title="Here is a title" id="ctl00_WebFormBody_txtPriceAdjustment" maxlength="3" name="ctl00$WebFormBody$txtPriceAdjustment">

So, when I click on my textbox, I want (for example) to do an alert... with the text that is within my label - so in this case it would alert "This is my label value"

所以,当我点击我的文本框时,我想(例如)用我标签内的文本做一个警报 - 所以在这种情况下它会提醒“这是我的标签值”

Hope that makes sense :)

希望这是有道理的:)

回答by Roko C. Buljan

Use the attribute selector []like [for='+ this.id +'], where this.idis the IDof the currently focused label

使用属性选择器,[]例如[for='+ this.id +'],当前edthis.idID在哪里focuslabel

$('input').on("focus", function() {
   var labelText = $('label[for='+  this.id  +']').text();
   console.log( labelText );  
});
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<label for="inp">This Is My Label Value</label>
<input  id="inp" type="text" >

回答by Tom

In a HTML code like this one:

在这样的 HTML 代码中:

<label for="input-email">Email</label>
<input type="text" name="input-email" value="" />

You can find the label content like this:

你可以找到这样的标签内容:

$('label[for="input-email"]').html();

回答by rlemon

$("#ctl00_WebFormBody_txtPriceAdjustment").bind("click",function(){
    alert($("label [for=" + this.id + "]").html());
});

or possibly

或者可能

alert($(this).closest("label").html());

depending on your markup you may just be able to select the next or previous siblings too.

根据您的标记,您也可以选择下一个或上一个兄弟姐妹。

回答by Shlomi Komemi

try this:

尝试这个:

$('input[type=text]').focus(function(){
     alert($('label[for=' + $(this).attr('id') + ']').html());
});

回答by Christopher

$('#ctl00_WebFormBody_txtPriceAdjustment').click(function() {
  alert($('#ctl00_WebFormBody_lblProductMarkup').text());
});

回答by Mattias Josefsson

To do it with javascript

用javascript来做

<script type="text/javascript">
function displayMessage()
{
alert(document.getElementById("ctl00_WebFormBody_lblProductMarkup").innerHTML);
}
</script>

<label id="ctl00_WebFormBody_lblProductMarkup"  for="ctl00_WebFormBody_txtPriceAdjustment">This Is My Label Value</label>

<input type="text" style="width:29px;" onclick="displayMessage()" title="Here is a title" id="ctl00_WebFormBody_txtPriceAdjustment" maxlength="3" name="ctl00$WebFormBody$txtPriceAdjustment">