使用jQuery查找所选控件或文本框的标签
我想要一些jQuery代码,当我点击文本框时,我可以找到控件的标签…所以在我的HTML中我有这个:
因此,当我点击我的文本框时,我希望(例如)使用我标签内的文本进行警告…所以在这种情况下它会提醒“这是我的标签值”
希望有道理:)
使用属性selector []
如[for='+ this.id +']
,其中this.id
是当前focus
label
的ID
$('input').on("focus", function() { var labelText = $('label[for='+ this.id +']').text(); console.log( labelText ); });
在像这样的HTML代码中:
你可以找到这样的标签内容:
$('label[for="input-email"]').html();
$("#ctl00_WebFormBody_txtPriceAdjustment").bind("click",function(){ alert($("label [for=" + this.id + "]").html()); });
或者可能
alert($(this).closest("label").html());
根据您的标记,您也可以选择下一个或上一个兄弟姐妹。
试试这个:
$('input[type=text]').focus(function(){ alert($('label[for=' + $(this).attr('id') + ']').html()); });
$('#ctl00_WebFormBody_txtPriceAdjustment').click(function() { alert($('#ctl00_WebFormBody_lblProductMarkup').text()); });
用javascript做