我想要一些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">所以,当我点击我的文本框时,我想(举个例子)做一个警告…使用我的标签中的文本-所以在本例中它会提示"This is my label value“
希望这是有意义的:)
发布于 2011-09-17 21:26:42
使用类似[for='+  this.id  +']的属性选择器[],其中this.id是当前focused label的ID
$('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" >
发布于 2011-09-17 21:24:26
在如下所示的HTML代码中:
<label for="input-email">Email</label>
<input type="text" name="input-email" value="" />您可以像这样查找标签内容:
$('label[for="input-email"]').html();发布于 2011-09-17 21:23:17
$("#ctl00_WebFormBody_txtPriceAdjustment").bind("click",function(){
    alert($("label [for=" + this.id + "]").html());
});或者有可能
alert($(this).closest("label").html());根据您的标记,您也可以选择下一个或上一个同级。
https://stackoverflow.com/questions/7455049
复制相似问题